<!DOCTYPE html>
<html lang="en-US">
  <head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width,initial-scale=1">
    <title>第1章-Linux系统编程入门 | whoway</title>
    <meta name="description" content="Personal Blog Website">
    <link rel="icon" href="/images/photo.jpg">
  <link rel="manifest" href="/images/photo.jpg">
  <link rel="apple-touch-icon" href="/images/photo.jpg">
  <meta http-quiv="pragma" cotent="no-cache">
  <meta http-quiv="pragma" cotent="no-cache,must-revalidate">
  <meta http-quiv="expires" cotent="0">
    
    <link rel="preload" href="/assets/css/0.styles.0dbae9ec.css" as="style"><link rel="preload" href="/assets/js/app.c70e21ad.js" as="script"><link rel="preload" href="/assets/js/72.e5467385.js" as="script"><link rel="prefetch" href="/assets/js/10.15222a53.js"><link rel="prefetch" href="/assets/js/100.7e0e5a86.js"><link rel="prefetch" href="/assets/js/101.efd59f25.js"><link rel="prefetch" href="/assets/js/102.dfbdc06c.js"><link rel="prefetch" href="/assets/js/103.d3ab2109.js"><link rel="prefetch" href="/assets/js/104.117957ef.js"><link rel="prefetch" href="/assets/js/105.046e8ff3.js"><link rel="prefetch" href="/assets/js/106.aebdc17d.js"><link rel="prefetch" href="/assets/js/107.248733c2.js"><link rel="prefetch" href="/assets/js/108.a2fecadc.js"><link rel="prefetch" href="/assets/js/109.35196857.js"><link rel="prefetch" href="/assets/js/11.770642f2.js"><link rel="prefetch" href="/assets/js/110.cf3d973c.js"><link rel="prefetch" href="/assets/js/111.f985889a.js"><link rel="prefetch" href="/assets/js/112.ad614f41.js"><link rel="prefetch" href="/assets/js/113.f666653c.js"><link rel="prefetch" href="/assets/js/114.c6c3f384.js"><link rel="prefetch" href="/assets/js/115.e51d3c2f.js"><link rel="prefetch" href="/assets/js/116.4f4b39f5.js"><link rel="prefetch" href="/assets/js/117.99352e11.js"><link rel="prefetch" href="/assets/js/118.c6ae6572.js"><link rel="prefetch" href="/assets/js/119.4ccbe778.js"><link rel="prefetch" href="/assets/js/12.042a92ff.js"><link rel="prefetch" href="/assets/js/120.edda1c4f.js"><link rel="prefetch" href="/assets/js/121.30a638ed.js"><link rel="prefetch" href="/assets/js/122.6efcefb1.js"><link rel="prefetch" href="/assets/js/123.91e6665b.js"><link rel="prefetch" href="/assets/js/124.f27e3d7e.js"><link rel="prefetch" href="/assets/js/125.c75712d5.js"><link rel="prefetch" href="/assets/js/126.ed756cce.js"><link rel="prefetch" href="/assets/js/127.2f06c74c.js"><link rel="prefetch" href="/assets/js/128.d5f6f30e.js"><link rel="prefetch" href="/assets/js/129.508b7eed.js"><link rel="prefetch" href="/assets/js/13.b5280c37.js"><link rel="prefetch" href="/assets/js/130.dc05f9aa.js"><link rel="prefetch" href="/assets/js/131.e0ba69b1.js"><link rel="prefetch" href="/assets/js/132.d79bcaa4.js"><link rel="prefetch" href="/assets/js/133.34acc01a.js"><link rel="prefetch" href="/assets/js/134.dabf64d5.js"><link rel="prefetch" href="/assets/js/135.ad90c915.js"><link rel="prefetch" href="/assets/js/136.dbb0074f.js"><link rel="prefetch" href="/assets/js/137.284ad365.js"><link rel="prefetch" href="/assets/js/138.a4b6856f.js"><link rel="prefetch" href="/assets/js/139.c9c1e20f.js"><link rel="prefetch" href="/assets/js/14.df02ba38.js"><link rel="prefetch" href="/assets/js/140.8b0a9269.js"><link rel="prefetch" href="/assets/js/141.9c7759c5.js"><link rel="prefetch" href="/assets/js/142.a4201a82.js"><link rel="prefetch" href="/assets/js/143.d7da6e8c.js"><link rel="prefetch" href="/assets/js/144.5e48e65d.js"><link rel="prefetch" href="/assets/js/145.a0e2633c.js"><link rel="prefetch" href="/assets/js/146.3c775f9b.js"><link rel="prefetch" href="/assets/js/147.22add89a.js"><link rel="prefetch" href="/assets/js/148.cfde1009.js"><link rel="prefetch" href="/assets/js/149.ffc835b5.js"><link rel="prefetch" href="/assets/js/15.fbdfc4ee.js"><link rel="prefetch" href="/assets/js/150.406c4b20.js"><link rel="prefetch" href="/assets/js/151.b2040eea.js"><link rel="prefetch" href="/assets/js/152.7bc65661.js"><link rel="prefetch" href="/assets/js/153.1d7c65e3.js"><link rel="prefetch" href="/assets/js/154.1309de49.js"><link rel="prefetch" href="/assets/js/155.81d3ee1f.js"><link rel="prefetch" href="/assets/js/156.154a4ef2.js"><link rel="prefetch" href="/assets/js/16.e5eb6147.js"><link rel="prefetch" href="/assets/js/17.57853c4a.js"><link rel="prefetch" href="/assets/js/18.cb9d7518.js"><link rel="prefetch" href="/assets/js/19.f354dc47.js"><link rel="prefetch" href="/assets/js/2.570d8a23.js"><link rel="prefetch" href="/assets/js/20.b5af7fad.js"><link rel="prefetch" href="/assets/js/21.0b1928fe.js"><link rel="prefetch" href="/assets/js/22.f78666de.js"><link rel="prefetch" href="/assets/js/23.29c3f366.js"><link rel="prefetch" href="/assets/js/24.6f596516.js"><link rel="prefetch" href="/assets/js/25.14067b60.js"><link rel="prefetch" href="/assets/js/26.74ba4989.js"><link rel="prefetch" href="/assets/js/27.13d60edd.js"><link rel="prefetch" href="/assets/js/28.9523cb32.js"><link rel="prefetch" href="/assets/js/29.8ec842e9.js"><link rel="prefetch" href="/assets/js/3.3fb3d2e0.js"><link rel="prefetch" href="/assets/js/30.805597a8.js"><link rel="prefetch" href="/assets/js/31.831b195d.js"><link rel="prefetch" href="/assets/js/32.063c672d.js"><link rel="prefetch" href="/assets/js/33.6d93fac3.js"><link rel="prefetch" href="/assets/js/34.56e8263c.js"><link rel="prefetch" href="/assets/js/35.dbe688bb.js"><link rel="prefetch" href="/assets/js/36.dc5af2c1.js"><link rel="prefetch" href="/assets/js/37.0a7494f6.js"><link rel="prefetch" href="/assets/js/38.fe4fc171.js"><link rel="prefetch" href="/assets/js/39.f5ed5e92.js"><link rel="prefetch" href="/assets/js/4.2c405ec8.js"><link rel="prefetch" href="/assets/js/40.fe7e2714.js"><link rel="prefetch" href="/assets/js/41.30b0811d.js"><link rel="prefetch" href="/assets/js/42.76f52d62.js"><link rel="prefetch" href="/assets/js/43.e7bb0817.js"><link rel="prefetch" href="/assets/js/44.ead0e883.js"><link rel="prefetch" href="/assets/js/45.235df046.js"><link rel="prefetch" href="/assets/js/46.5f09e829.js"><link rel="prefetch" href="/assets/js/47.67116354.js"><link rel="prefetch" href="/assets/js/48.31f34543.js"><link rel="prefetch" href="/assets/js/49.10b5ebba.js"><link rel="prefetch" href="/assets/js/5.6f47322c.js"><link rel="prefetch" href="/assets/js/50.c0f0b7f1.js"><link rel="prefetch" href="/assets/js/51.5143f3bf.js"><link rel="prefetch" href="/assets/js/52.eeddfd48.js"><link rel="prefetch" href="/assets/js/53.eb790db5.js"><link rel="prefetch" href="/assets/js/54.8fe5421c.js"><link rel="prefetch" href="/assets/js/55.d8f9004b.js"><link rel="prefetch" href="/assets/js/56.62ac9b92.js"><link rel="prefetch" href="/assets/js/57.a9caec0d.js"><link rel="prefetch" href="/assets/js/58.f93fc522.js"><link rel="prefetch" href="/assets/js/59.a81a03aa.js"><link rel="prefetch" href="/assets/js/6.8c2ea393.js"><link rel="prefetch" href="/assets/js/60.ab782775.js"><link rel="prefetch" href="/assets/js/61.6dd12daf.js"><link rel="prefetch" href="/assets/js/62.76f4b01f.js"><link rel="prefetch" href="/assets/js/63.6f8a4742.js"><link rel="prefetch" href="/assets/js/64.6f8bb1fa.js"><link rel="prefetch" href="/assets/js/65.4120a44b.js"><link rel="prefetch" href="/assets/js/66.360c2d2b.js"><link rel="prefetch" href="/assets/js/67.26f84d32.js"><link rel="prefetch" href="/assets/js/68.68f45e5e.js"><link rel="prefetch" href="/assets/js/69.e311eb56.js"><link rel="prefetch" href="/assets/js/7.6762b2d7.js"><link rel="prefetch" href="/assets/js/70.cea82674.js"><link rel="prefetch" href="/assets/js/71.783ddcf7.js"><link rel="prefetch" href="/assets/js/73.b8fb681b.js"><link rel="prefetch" href="/assets/js/74.1bae37db.js"><link rel="prefetch" href="/assets/js/75.024387e5.js"><link rel="prefetch" href="/assets/js/76.a8e53010.js"><link rel="prefetch" href="/assets/js/77.8c55500a.js"><link rel="prefetch" href="/assets/js/78.7ce90bf5.js"><link rel="prefetch" href="/assets/js/79.ef71713f.js"><link rel="prefetch" href="/assets/js/8.788a6364.js"><link rel="prefetch" href="/assets/js/80.acad589d.js"><link rel="prefetch" href="/assets/js/81.02670d10.js"><link rel="prefetch" href="/assets/js/82.53b7b1ac.js"><link rel="prefetch" href="/assets/js/83.99eb8581.js"><link rel="prefetch" href="/assets/js/84.d1535ce3.js"><link rel="prefetch" href="/assets/js/85.fe2b7de9.js"><link rel="prefetch" href="/assets/js/86.41850272.js"><link rel="prefetch" href="/assets/js/87.1cdc6df9.js"><link rel="prefetch" href="/assets/js/88.01bf3461.js"><link rel="prefetch" href="/assets/js/89.17c69819.js"><link rel="prefetch" href="/assets/js/9.3813842d.js"><link rel="prefetch" href="/assets/js/90.f6ae7e35.js"><link rel="prefetch" href="/assets/js/91.507bc284.js"><link rel="prefetch" href="/assets/js/92.90551782.js"><link rel="prefetch" href="/assets/js/93.dc442d78.js"><link rel="prefetch" href="/assets/js/94.315f4e94.js"><link rel="prefetch" href="/assets/js/95.ccd6c6bf.js"><link rel="prefetch" href="/assets/js/96.0c6d89d0.js"><link rel="prefetch" href="/assets/js/97.1a9f10a9.js"><link rel="prefetch" href="/assets/js/98.43be3caa.js"><link rel="prefetch" href="/assets/js/99.54c8207b.js">
    <link rel="stylesheet" href="/assets/css/0.styles.0dbae9ec.css">
  </head>
  <body>
    <div id="app" data-server-rendered="true"><div class="theme-container"><header class="navbar"><div class="sidebar-button"><svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" role="img" viewBox="0 0 448 512" class="icon"><path fill="currentColor" d="M436 124H12c-6.627 0-12-5.373-12-12V80c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12zm0 160H12c-6.627 0-12-5.373-12-12v-32c0-6.627 5.373-12 12-12h424c6.627 0 12 5.373 12 12v32c0 6.627-5.373 12-12 12z"></path></svg></div> <a href="/" class="home-link router-link-active"><!----> <span class="site-name">whoway</span></a> <div class="links" style="max-width:nullpx;"><div class="search-box"><input aria-label="Search" autocomplete="off" spellcheck="false" value=""> <!----></div> <nav class="nav-links can-hide"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link router-link-active">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav></div></header> <div class="sidebar-mask"></div> <div class="sidebar"><nav class="nav-links"><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🎓Coding</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/00.Coding/TheBeautyOfProgramming/" class="nav-link">🔖编程之美题解</a></li><li class="dropdown-item"><!----> <a href="/00.Coding/CodeWarehouse/" class="nav-link">🔖代码意识流</a></li></ul></div></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">🚀语言</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/01.Language/Overview/" class="nav-link">🔖概述</a></li><li class="dropdown-item"><!----> <a href="/01.Language/C/" class="nav-link">⭐️C</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Cpp/" class="nav-link">🚀C++</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Java/" class="nav-link">☕️Java</a></li><li class="dropdown-item"><!----> <a href="/01.Language/Python/" class="nav-link">🧩Python3</a></li></ul></div></div><div class="nav-item"><a href="/02.Hardware/" class="nav-link">✔️硬件基础</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⭐️软件基础</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/03.Software/01.DataStructureAndAlgorithm/" class="nav-link">🐾数据结构和算法</a></li><li class="dropdown-item"><!----> <a href="/03.Software/02.OS/" class="nav-link router-link-active">💻操作系统</a></li><li class="dropdown-item"><!----> <a href="/03.Software/03.Net/" class="nav-link">☁️计算机网络</a></li><li class="dropdown-item"><!----> <a href="/03.Software/04.SE/" class="nav-link">✅软件工程</a></li></ul></div></div><div class="nav-item"><a href="/04.Database/" class="nav-link">🎨数据库</a></div><div class="nav-item"><a href="/05.Engineer/" class="nav-link">🔖学术/工程</a></div><div class="nav-item"><div class="dropdown-wrapper"><a class="dropdown-title"><span class="title">⚙️工具</span> <span class="arrow right"></span></a> <ul class="nav-dropdown" style="display:none;"><li class="dropdown-item"><!----> <a href="/06.Tools/01.employment/" class="nav-link">🔖求职</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/02.efficiency/" class="nav-link">🚀效能</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/03.windows/" class="nav-link">⚙️Windows</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/04.design/" class="nav-link">🧩设计</a></li><li class="dropdown-item"><!----> <a href="/06.Tools/05.linux/" class="nav-link">🐉Linux</a></li></ul></div></div><div class="nav-item"><a href="https://github.com/whoway" target="_blank" rel="noopener noreferrer" class="nav-link external">
  GitHub
  <svg xmlns="http://www.w3.org/2000/svg" aria-hidden="true" x="0px" y="0px" viewBox="0 0 100 100" width="15" height="15" class="icon outbound"><path fill="currentColor" d="M18.8,85.1h56l0,0c2.2,0,4-1.8,4-4v-32h-8v28h-48v-48h28v-8h-32l0,0c-2.2,0-4,1.8-4,4v56C14.8,83.3,16.6,85.1,18.8,85.1z"></path> <polygon fill="currentColor" points="45.7,48.7 51.3,54.3 77.2,28.5 77.2,37.2 85.2,37.2 85.2,14.9 62.8,14.9 62.8,22.9 71.5,22.9"></polygon></svg></a></div> <!----></nav>  <ul class="sidebar-links"><li><div class="sidebar-group first"><p class="sidebar-heading open"><span>第1章-Linux系统编程入门</span> <!----></p> <ul class="sidebar-group-items"><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_0-课程介绍" class="sidebar-link">0.课程介绍</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_1-为什么要学习这门课？" class="sidebar-link">1.为什么要学习这门课？</a></li><li class="sidebar-sub-header"><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_2-课程内容" class="sidebar-link">2.课程内容</a></li></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_1-linux开发『环境搭建』" class="sidebar-link">1.Linux开发『环境搭建』</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_2-3-gcc" class="sidebar-link">2-3.GCC</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_4-9-静态库和动态库" class="sidebar-link">4-9.静态库和动态库</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_10-12-makefile『redis？』" class="sidebar-link">10-12.Makefile『Redis？』</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_13-gdb调试-1" class="sidebar-link">13-GDB调试(1)</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_14-16-gdb调试-2" class="sidebar-link">14-16.GDB调试(2)</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_17-标准c库io函数和linux系统io函数对比✅" class="sidebar-link">17-标准C库IO函数和Linux系统IO函数对比✅</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_18-虚拟地址空间" class="sidebar-link">18-虚拟地址空间</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_19-文件描述符" class="sidebar-link">19-文件描述符</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_20-open打开文件" class="sidebar-link">20-open打开文件</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_21-open创建新文件" class="sidebar-link">21-open创建新文件</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_22-read和write函数h" class="sidebar-link">22.read和write函数h</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_23-lseek函数" class="sidebar-link">23.lseek函数</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_24-stat和lstat函数" class="sidebar-link">24.stat和lstat函数</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#『1』stat-c" class="sidebar-link">『1』stat.c</a></li></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_25-模拟实现ls-l命令" class="sidebar-link">25.模拟实现ls -l命令</a><ul class="sidebar-sub-headers"><li class="sidebar-sub-header"><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#『1』ls-l-c" class="sidebar-link">『1』ls-l.c</a></li></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_26-文件属性操作函数" class="sidebar-link">26.文件属性操作函数</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_27-目录操作函数" class="sidebar-link">27.目录操作函数</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_28-目录遍历函数" class="sidebar-link">28-目录遍历函数</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_29-dup和dup2函数" class="sidebar-link">29.dup和dup2函数</a><ul class="sidebar-sub-headers"></ul></li><li><a href="/03.Software/02.OS/server/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8/%E7%AC%AC1%E7%AB%A0-Linux%E7%B3%BB%E7%BB%9F%E7%BC%96%E7%A8%8B%E5%85%A5%E9%97%A8.html#_30-fcntl函数" class="sidebar-link">30.fcntl函数</a><ul class="sidebar-sub-headers"></ul></li></ul></div></li></ul> </div> <div class="page"> <div class="content"><h1 id="第1章-linux系统编程入门"><a href="#第1章-linux系统编程入门" class="header-anchor">#</a> 第1章-Linux系统编程入门</h1> <ul><li>牛客—高境</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token operator">&lt;</span>font style<span class="token operator">=</span><span class="token string">&quot;background:yellow&quot;</span><span class="token operator">&gt;</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><h2 id="_0-课程介绍"><a href="#_0-课程介绍" class="header-anchor">#</a> 0.课程介绍</h2> <h3 id="_1-为什么要学习这门课？"><a href="#_1-为什么要学习这门课？" class="header-anchor">#</a> 1.为什么要学习这门课？</h3> <ul><li>C++相关岗位开发非常多：比如，Linux服务器开发、桌面客户端开发、游戏开发、网络安全方向的、音视频开发，非常多</li> <li>调研：
<ul><li>大多数是做Linux服务器开发，因为他的相关岗位最多，相对其他岗位比较容易上手的。这个是学习这门课的第1个原因</li> <li>第2个原因：求职过程当中，和Linux服务开发相关的面试题也是最多的。</li></ul></li> <li>在这，我给大家整理了在『面试当中』经常会问到的一些面试题，比如说，</li></ul> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>1．进程间通信的方式?	什么是进程，什么是进程间通信，有哪几种，原理是什么？
2．僵尸进程，孤儿进程
3．线程同步怎么解决? 什么是线程同步，怎么去解决它呢？
4．大端和小端的区别?	和网络相关
5.Io多路复用有哪些方式?区别?和特点
6．静态库跟共享库的制作以及使用它们的原理和区别
7．滑动窗口的机制
8. TCP三次握手，四次挥手
9.TCP和UDP的区别
...
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><p>如果说同学们，想要掌握这些面试中的知识点，可能需要学习如下的几本书：</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>参考书籍:
Linux系统编程相关，这里面还包括了Linux多线程，多进程的开发
《UNIX环境高级编程第3版》
《Linux/UNIX系统编程手册》
《Linux内核设计与实践》

//Linux网络编程相关的书籍，
《计算机网络第7版》
《TCP/IP详解卷1》
《UNIX网络编程第3版》
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br></div></div><ul><li>很多同学在学习这些内容之中，他们并不知道我们要先学什么，再学啥。也不知道哪些是必须掌握的重点，很多同学对这个没有体系。为了让大家在短时间里面。快速的掌握Linux服务器开发相关的知识</li> <li>以及能够让大家在面试的过程中遇到这些问题的时候，能够很准确的去回答出来</li></ul> <p>所以，我给大家准备了这门课程。这个是大家学习这门课程的第2个原因。</p> <h3 id="_2-课程内容"><a href="#_2-课程内容" class="header-anchor">#</a> 2.课程内容</h3> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>Linux高并发服务器开发

第1章Linux系统编程入门
	介绍开发环境、GCC、Linux相关的一些基本函数！
	让大家快速的掌握，如何在linux环境中进行开发
第2章Linux多进程开发
	介绍，进程操作，
	重点：进程间的通信方式！这是面试中常问的！
第3章Linux多线程开发
	介绍，线程的分离，啥的，
	重点：线程间的同步，这是面试中经常会问到的！
第4章Linux网络编程
	Linux网络编程相关的一些API，还有一些网络模型，协议啊。
	最重要的：TCP通信的原理，以及，最重要的就是IO多路复用等等。
第5章项目实战与总结
	利用上面4章的内容，开发一个Web服务器。
	介绍：Linux服务器开发的一些其他的内容。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br></div></div><p>本机写的代码，能够同步到远程Linux当中，提高开发效率</p> <ul><li>25分钟40秒『VScode远程连接虚拟机』</li> <li>安装一些插件：</li></ul> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code>Chinese语言包，将英语变为中文
Remote Development	远程连接到服务器，安装后发现多了一个东西
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br></div></div><h2 id="_1-linux开发『环境搭建』"><a href="#_1-linux开发『环境搭建』" class="header-anchor">#</a> 1.Linux开发『环境搭建』</h2> <blockquote><p>重要，省略</p></blockquote> <h2 id="_2-3-gcc"><a href="#_2-3-gcc" class="header-anchor">#</a> 2-3.GCC</h2> <ul><li>（整理到学术/工程）</li></ul> <h2 id="_4-9-静态库和动态库"><a href="#_4-9-静态库和动态库" class="header-anchor">#</a> 4-9.静态库和动态库</h2> <ul><li>（整理到学术/工程）</li> <li>04-静态库的制作</li> <li>05-静态库的使用</li> <li>06-动态库的制作和使用</li> <li>07-动态库加载失败的原因</li> <li>08-解决动态库加载失败的问题</li> <li>09-静态库和动态库的对比</li></ul> <h2 id="_10-12-makefile『redis？』"><a href="#_10-12-makefile『redis？』" class="header-anchor">#</a> 10-12.Makefile『Redis？』</h2> <ul><li>（整理到学术/工程）</li></ul> <h2 id="_13-gdb调试-1"><a href="#_13-gdb调试-1" class="header-anchor">#</a> 13-GDB调试(1)</h2> <h4 id="main-cpp"><a href="#main-cpp" class="header-anchor">#</a> main.cpp</h4> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&quot;sort.h&quot;</span></span>

<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">int</span> array<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">12</span><span class="token punctuation">,</span> <span class="token number">27</span><span class="token punctuation">,</span> <span class="token number">55</span><span class="token punctuation">,</span> <span class="token number">22</span><span class="token punctuation">,</span> <span class="token number">67</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> len <span class="token operator">=</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>array<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">bubbleSort</span><span class="token punctuation">(</span>array<span class="token punctuation">,</span> len<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 遍历</span>
    cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;冒泡排序之后的数组: &quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        cout <span class="token operator">&lt;&lt;</span> array<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">&lt;&lt;</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    cout <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>
    cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;===================================&quot;</span> <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>

    <span class="token keyword">int</span> array1<span class="token punctuation">[</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">25</span><span class="token punctuation">,</span> <span class="token number">47</span><span class="token punctuation">,</span> <span class="token number">36</span><span class="token punctuation">,</span> <span class="token number">80</span><span class="token punctuation">,</span> <span class="token number">11</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    len <span class="token operator">=</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>array1<span class="token punctuation">)</span> <span class="token operator">/</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span><span class="token keyword">int</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token function">selectSort</span><span class="token punctuation">(</span>array1<span class="token punctuation">,</span> len<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token comment">// 遍历</span>
    cout <span class="token operator">&lt;&lt;</span> <span class="token string">&quot;选择排序之后的数组: &quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        cout <span class="token operator">&lt;&lt;</span> array1<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">&lt;&lt;</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    cout <span class="token operator">&lt;&lt;</span> endl<span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br></div></div><h4 id="select-cpp"><a href="#select-cpp" class="header-anchor">#</a> select.cpp</h4> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&quot;sort.h&quot;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>

<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">void</span> <span class="token function">selectSort</span><span class="token punctuation">(</span><span class="token keyword">int</span> <span class="token operator">*</span>array<span class="token punctuation">,</span> <span class="token keyword">int</span> len<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> len <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len<span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">if</span> <span class="token punctuation">(</span>array<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">&gt;</span> array<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				<span class="token keyword">int</span> temp <span class="token operator">=</span> array<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
				array<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> array<span class="token punctuation">[</span>i<span class="token punctuation">]</span><span class="token punctuation">;</span>
				array<span class="token punctuation">[</span>i<span class="token punctuation">]</span> <span class="token operator">=</span> temp<span class="token punctuation">;</span>
			<span class="token punctuation">}</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h4 id="sort-h"><a href="#sort-h" class="header-anchor">#</a> sort.h</h4> <div class="language-h line-numbers-mode"><pre class="language-text"><code>#ifndef SORT_H_
#define SORT_H_

void bubbleSort(int *array, int len);

void selectSort(int *array, int len);

#endif
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br></div></div><h4 id="test-c"><a href="#test-c" class="header-anchor">#</a> test.c</h4> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span><span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> a<span class="token punctuation">,</span> b<span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;argc = %d\n&quot;</span><span class="token punctuation">,</span> argc<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>argc <span class="token operator">&lt;</span> <span class="token number">3</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        a <span class="token operator">=</span> <span class="token number">10</span><span class="token punctuation">;</span>
        b <span class="token operator">=</span> <span class="token number">30</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> <span class="token keyword">else</span> <span class="token punctuation">{</span>
        a <span class="token operator">=</span> <span class="token function">atoi</span><span class="token punctuation">(</span>argv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        b <span class="token operator">=</span> <span class="token function">atoi</span><span class="token punctuation">(</span>argv<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;a = %d, b = %d\n&quot;</span><span class="token punctuation">,</span> a<span class="token punctuation">,</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;a + b = %d\n&quot;</span><span class="token punctuation">,</span> a <span class="token operator">+</span> b<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> a<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;i = %d\n&quot;</span><span class="token punctuation">,</span> i<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token comment">// 函数调用</span>
        <span class="token keyword">int</span> res <span class="token operator">=</span> <span class="token function">test</span><span class="token punctuation">(</span>i<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;res value: %d\n&quot;</span><span class="token punctuation">,</span> res<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;THE END !!!\n&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token keyword">int</span> <span class="token function">test</span><span class="token punctuation">(</span><span class="token keyword">int</span> a<span class="token punctuation">)</span> <span class="token punctuation">{</span>
    <span class="token keyword">int</span> num <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>
    <span class="token keyword">for</span><span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> a<span class="token punctuation">;</span> <span class="token operator">++</span>i<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        num <span class="token operator">+=</span> i<span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token keyword">return</span> num<span class="token punctuation">;</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br></div></div><h4 id="bubble-cpp"><a href="#bubble-cpp" class="header-anchor">#</a> bubble.cpp</h4> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&quot;sort.h&quot;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;iostream&gt;</span></span>

<span class="token keyword">using</span> <span class="token keyword">namespace</span> std<span class="token punctuation">;</span>

<span class="token keyword">void</span> <span class="token function">bubbleSort</span><span class="token punctuation">(</span><span class="token keyword">int</span> <span class="token operator">*</span>array<span class="token punctuation">,</span> <span class="token keyword">int</span> len<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> i <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> i <span class="token operator">&lt;</span> len <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">;</span> i<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
		<span class="token keyword">for</span> <span class="token punctuation">(</span><span class="token keyword">int</span> j <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span> j <span class="token operator">&lt;</span> len <span class="token operator">-</span> <span class="token number">1</span> <span class="token operator">-</span> i<span class="token punctuation">;</span> j<span class="token operator">++</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
			<span class="token keyword">if</span> <span class="token punctuation">(</span>array<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">&gt;</span> array<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
				<span class="token keyword">int</span> temp <span class="token operator">=</span> array<span class="token punctuation">[</span>j<span class="token punctuation">]</span><span class="token punctuation">;</span>
				array<span class="token punctuation">[</span>j<span class="token punctuation">]</span> <span class="token operator">=</span> array<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
				array<span class="token punctuation">[</span>j <span class="token operator">+</span> <span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> temp<span class="token punctuation">;</span>
			<span class="token punctuation">}</span>
		<span class="token punctuation">}</span>
	<span class="token punctuation">}</span>

<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h2 id="_14-16-gdb调试-2"><a href="#_14-16-gdb调试-2" class="header-anchor">#</a> 14-16.GDB调试(2)</h2> <h2 id="_17-标准c库io函数和linux系统io函数对比✅"><a href="#_17-标准c库io函数和linux系统io函数对比✅" class="header-anchor">#</a> 17-<code>标准C库IO函数</code>和Linux<code>系统IO函数</code>对比✅</h2> <blockquote><p>接下来，我们去学习一下Linux中的文件IO，也就是文件的输入、输出操作</p></blockquote> <p>那么在这儿呢首先呢给大家去解释一下这个名词啊，就是这个io啊。
那很多同学呢可能啊，也学过这个标准c库的文件io啊，但是很多人呢不能够去理解什么是这个io啊。</p> <p>那么文件输入和输出啊，那什么是文件输入？什么又是文件输出呢？
那这样呢我们要啊看站在哪个『角度』了啊！</p> <blockquote><p>文件的角度</p></blockquote> <p>那比如说呢我们是站在这个文件的角度啊，比如说有一个文件，对吧？
那么对于文件来说，它的这个输入什么呀？是不是从外面把这个数据啊写到我这个文件里面，对吧？
那这个外面是哪儿呢？其实就是我们的『内存』啊，这是我们的内存，这个叫做输入。
那输出呢输出就是从我们这个文件呢把数据呢给读取到内存里面，对吧？
这是站在文件的角度去看这个输入和输出的啊。</p> <blockquote><p>内存的角度</p></blockquote> <p>那么如果说我们站在这个内存的角度啊，比如说这是内存这是一个文件，对吧？
那站在内存的角度，什么是输入呢？
是不是从文件里面把数据读取到内存里面，对吧？
那输出呢就是从内存里面呢把数据写到文件当中啊。</p> <p>那你站在这个角度的不同呢，其实这个输入和输出它的这个呃理解呢也不一样，对吧？</p> <ul><li>那我们应该是站在哪个角度去看这个输入和输出呢？</li> <li>啊注意了，『应该是站在这个内存』的角度啊，因为我们是写的这个程序，对吧？</li></ul> <p>那程序呢它是在我们这个内存当中啊，就是我们运行程序的话呢会加载到内存里面，对吧？
它是在这个内存里面呢去运行的啊也就是加载到内存里面，对吧？</p> <p>那么呃我们站在内存的角度去看的话，输
入呢就是从文件里面去读取数据到内存里面，对吧？
输入嘛输入到内存里面来嘛。	
那么输出的话呢就是从内存里面呢把数据写到文件里面啊，就是输出啊。
所以这个io啊这两个单词啊大家一定要能够去理解啊！
什么是输入，什么是输出？
我们呢是站在这个内存的角度去看待的！</p> <blockquote><p>好，那么接下来呢我们要去学习的是这个linux当中的文件io啊。</p></blockquote> <p>那在这儿呢我们呃，，先来啊对这个linux系统当中的这个呃io啊，，io操作啊，或者说呢就是这个文件io的一些函数啊，
和同学们呢之前啊学过的这个标准c库的文件io啊，
来做一个比较啊那同学们学过这个c语言，那肯定呢学过这个c语言当中这个标准的c库的i、o，
那我们啊呃要对他们呢进行一个比较啊，我们得知道他们之间呢有什么地方是相同的，什么是不同的，对吧？</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>![01.IO](./img/1.17/01.IO.png)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><p>那么这儿呢有一张图啊，这张图呢就是标准的c库io函数啊相关的一些内容啊。
那么在这个左边呢大家来看一下，左边呢是我们常用的一些这个标准c库的啊这个io函数，比如说fopen去打开一个文件，对吧？</p> <p>那这个呢是同学们呢呃之前已经学习过的这个标准c库的io函数啊。
那么呃首先呢他们这个『标准c库io函数啊』跟这个『linux系统的啊这个文件io函数啊』呃</p> <ul><li>1、第一个区别呢就是在于标准c库的io函数呢它是可以跨平台的啊。
那什么叫跨平台啊什么叫跨平台呢？就是啊我们啊呃用这个标准c库这些函数啊开发的程序呢啊在各个平台上呢都能够运行。</li> <li>2、啊那什么是平台？
所谓平台呢就是不同的操作系统嘛，对吧？
那跨平台呢就是跨操作系统啊
比如说我开发了一个这个呃我用这个c函数库啊去开发了一个程序啊，那我可以在这个windows系统当中呢去运行啊，
也可以呢在这个linux系统啊，或者呢在这个mac系统当中去运行。</li> <li>3、那这个呢就是啊跨平台啊那么还有像大家学习的这个▁q t啊，他呢也是这个跨平台的啊。
那么这种跨平台它是怎么去实现的呢？
啊那么一般呢这个呃跨平台呢『它实现的方式呢无非就两种』啊，
（1）那一种呢就是呃像这个java啊java那样，它有一个这个java虚拟机对吧？
那么。它为每一个这个平台呢都开发了一个这个加法虚拟机啊而这个加法程序呢哎你在任何平台上呢写的这个代码呢都是一样的啊
那么跑的时候呢它也是跑在这个加法虚拟机里面的啊那么不同的平台呢，这个加法虚拟机啊它的实现呢也是不同的，对吧？
（2）那么呃这个我们像我们这个标准c库啊，其实我们呃可以称之为什么呢第3方库啊，就是标准c库啊我们可以称之为第三方库，
也就是说它不属于操作系统的啊它不属于操作系统。那么它的这个跨平台是怎么实现的呢？
那么它的这个跨平台呢，『其实呃它是在这个各个平台上啊调用了不同平台的这个呃『系统api』啊
比如说呢我们在这个windows平台上啊使用这个fopen打开一个文件，对吧？那么他呢会呃调用这个windows系统的这个api呢，去打开一个文件啊。
就是呃这个库呢它在封装的时候呢针对不同的这个平台啊，采用不同平台的这个api啊去实现。
那这个是呃给大家介绍的两种啊实现跨平台的这个方式啊。</li> <li>4、那么也就是说啊我们这个标准c库的io函数啊，它呢其实是呃要『高于』我们接下来要给大家介绍的这个『linux系统io函数』
那他们之间呢。是什么关系呢？
其实它们之间呢是调用和被调用的关系啊。
比如说呢我在这个linux的平台下，我调用了一个呃标准c库的这个fopen函数去打开一个文件，对吧？
那么他呢能够去打开文件，其实底层呢它会先调用这个linux啊系统的一个api，叫open函数啊，后面呢会给大家介绍啊。
我们说啊这个linux系统的io呢啊这个io函数或者ioapi啊，它呢是『更加偏底层的啊，或者呢说更加低级一些。』</li> <li>5、而这个标准c库啊它的这些io函数啊它是更加高级的啊。
那么高级呢呃『首先它的这个效率呢是要高的啊』那这个呢啊我们一会再说。</li> <li>6、啊那一般呢我们写这个程序啊，呃肯定呢是建议大家呢去使用这个标准c库的io函数啊，而不能说我们呃学习了这个linux的一个系统l函数，
那我们就去使用这个linux的这个系统IO函数啊，不是这 样的啊，我们要根据不同的这个场景呢去使用啊！！</li> <li>7、那我我刚才也说了，『这个标准c库啊它的这个io函数呢它的效率呢是要高的』，对吧？</li> <li>啊那为什么要高呢？</li> <li>因为它里面呢呃是带有这个缓冲区的啊缓冲区的。</li></ul> <p>那我们什么时候去使用这个linux系统io函数呢？？
啊其实它也是有这个使用场景的啊，
你比如说这个网络通信的时候，对吧？
那这个呢是我们后面啊也会给大家介绍啊，给大家去学习这个呃linux当中的网络通信啊。
那现在呢我们还没有学到啊，我们来看一下这个图啊。
那么这个图呢比如说左边呢是这个标准的一个c库的一个函数，是吧？</p> <p>它返回的是一个file文件指针，对吧？返回的是一个文件指针啊。
那其实我们可以通过这个呃linux啊，它的自带的一个帮助文档啊。
man。
好，那么这个。『标准c库啊，它的api呢都是在这个第三章里面啊』
man 3啊然后呢呃fopen，大家来看一下这个apple open它的返回值是不是返回的是一个FILE * 啊，对吧返回的是一个发小新啊。</p> <hr> <p>好，那么在这儿呢我们呃要搞清楚啊这个FILE *里面它里面呢都有哪些东西，对吧？
那么这个FILE文件指针里面，它其实呃就是一个结构体啊那这里面呢主要有呃三个部分比较重要的啊，需要大家知道的那一个呢是。
这个文件描述符啊文件描述符。
那注意了，这个文件描述符呢我们啊接下来呢啊要经常跟他去打交道啊，那它呢其实就是一个整数值，对吧？
那这个文件描述符是用来干嘛的呢？
它是用来指向一个已经打开的文件啊!!
比如说我们调用apple，对吧，我们去打开一个文件，那么
这个时候呢他就会在这个结构体里面就会有一个文件描述符这个整数值啊指向你这个文件啊指向你这个回的文件。
啊这个就是文件描述符它的一个作用啊，它是用来定位这个文件用的啊!!!!!</p> <ul><li>那这个文件描述符呢呃在我们这个linux系统当中啊，我们一般称之为文件描述符啊</li> <li>在这个windows系统当中呢呃一般呢我们称之为这个文件句柄啊这个呢大家了解一下。
然后呢在这个FILE结构体当中啊。
第二个比较重要的就是这个文件的读写指针
那我们看这个名字也能够知道它是干嘛用的，，是用来读取数据和写数据的，对吧？
读取数据和写数据啊，，其实就是操作我们这个数据的啊，
其实在这个里面呢它维护了啊两个指针，用于你去写数据啊其实在这个里面呢
它维护了两个指针用于你去啊啊那么然后呢还有呃</li></ul> <p>第三块比较重要的就是这个io的一个缓冲区啊io的缓冲区。</p> <p>那么呃这个io缓冲区它是干嘛用的呢？</p> <ul><li>就说咱们啊所学习的『所有的啊标准的c库io函数呢，它们其实都是带缓冲区的啊』什么叫缓冲区呢？
呃比如说我们使用这个fwrite啊去往我们这个。文件里面去写数据，对吧？
那你写数据的时候啊，我们是我们可以去看一下这个呃帮助文档啊。
它有几个参数，第一个呢是呃一个指针，这是我们要写的一个数据，对吧？
第二个呢是size_t，这个呢是其实就是呃这个块的一个大小啊，
每个呃单个字节的一个就是单个块的一个大小，就是说『指前面这个数据单个块的一个大小』』
然后后面一个这个size_t啊然后nmeb，这个呢其实就是『每一个块的啊具体的一个这个数量，就是说我到底有多少个这个块，对吧？』
比如说我们往这个文件当中啊去呃写一个这个字符串，对吧？
那么字符串里面每一块其实就是一个字符嘛。
对吧？
其实它是占一个字节，对吧？
那么有多少个字节呢？
其实就是有多少个这个字符，对不对啊？
是这两个参数。</li></ul> <hr> <blockquote><p>什么是缓冲区，缓冲区的作用
是不是就是提高我们这个啊执行的一个效率啊？
然后这个我们还要指定这个file*啊，这是文件指针嘛，对吧？
我们要通过这个文件指针去操作那个文件嘛，对吧？
在这个文件指针里面，它我们刚才说它维护了什么他维护了这个啊文件描述符啊，文件的这个读写指针，对吧？
文件描述符呢是找到这个文件用的，文文件读写指针呢是操作这个文件的数据的。
啊还有这个缓冲区啊。
那你比如说我们往这个里面呢去写一个数据，那你说它是一下子就把这个数据啊这个写到磁盘里面嘛？？
啊它不是这样的啊它不是这样的，它里面呢它会有一个缓冲区！！！
那你写的时候啊，比如说你写了多次啊写了多次，，把这个数据呢都写到这个缓冲区里面了啊然后呢。
呃他这个缓冲区啊如果说已经满了啊，他就会把这个数据啊调用一次我们这个linux的一个系统啊io函数啊，
把这个数据呢啊写一次写到这个磁盘里面啊写到磁盘里面，所以说这个就是缓冲区。</p></blockquote> <ul><li>那缓冲区的作用什么呢？</li> <li>大家想一想，缓冲区的作用是不是就是提高我们这个啊执行的一个效率啊？
对吧？执行我们这个程序的一个效率。</li></ul> <p>好，那大家想想啊，比如说啊我们呃要从一个地方把这个数据啊运动就是运到另一个地方，对吧？
那如果说你你每一次运的话，你写一次，对吧你运一次啊把一个数据过去啊运一次把一个数据过去啊。
然后呢还有一种方式就是我们有一个缓冲区啊，你把数据呢先写到这个缓冲区里面，然后再一次把这些数据放到这个磁盘里面，对吧？
因为我。我们知道是不是要操作这个磁盘啊，往磁盘里面去写数据吧，是不是要跟这个硬件打打交道了，对不对？
那么『果说呃你这个呃频繁地去操作这个硬件的话，那它的这个效率呢肯定是啊没有这个缓冲区要高的』
大家想一想是不是这样的，对吧啊那么在这儿呢，其实这个缓冲区啊，，它什么时候往我们这个磁盘里面写数据呢？
啊一个呢就是我们，，这个缓冲区里面的数据满了，那么它会往我们这个磁盘里面呢去写入数据。
啊那么还有呢我们可以通过强制调这个啊fflush，就是强制的去刷新你这个缓冲区，把你缓冲区里面的内容呢哎写到磁盘里面啊
或者呢我们呃比如说呢正常关闭啊，我们调用这个a，class对吧？
of，close。
或者呢我们函数呢结束了，对吧？
exit退出函数了啊。
会先把这个缓冲区里面的数据啊写到磁盘，对吧？
因为如果说你不写的话啊，你就把程序停掉了！！那么是不是有些数据就没有写到磁盘里面，是吧？
啊所以说他要做这样的一些操作啊。</p> <hr> <ul><li>那么呃刚才也说了，这个缓冲区呢它能够啊提高我们这个效率啊那么提高的效率它主要是提高在哪啊？</li> <li>是不就是『降低了我们写磁盘的一个次数』，对吧？
原先呢我们写的话啊一次呢哎写到这个磁盘里面，你就要去啊写我们这个磁盘，对吧现在呢我有了缓冲区，对吧你就要去啊写我们这个磁盘对吧现在呢我有了缓冲区，对吧？
你就要去啊写我们这个磁盘，对吧现在呢我有了缓冲区，对吧？
你就要去啊写我们这个磁盘，对吧？</li></ul> <p>好，然后呢再到在这儿呢大家还要知道——
就是呃关于我们这个缓冲区啊它的一个大小呢啊它默认呢是这个呃8192个字节啊，大概呢就是8K，
对吧大概就是八k啊那么当然了，这个是可以调节的啊，但是呢不建议大家去调节啊！！！
因为这个呢是一个理想的这个大小啊，，就是说呃这个开发人员啊他这个试过了大概呢是八k的时候呢，他的这个效率呢是最高的啊。
好了，那咱们总结一下啊就是说呃标准c库io函数啊它是呃带有这个缓冲区的，对吧？它呢能够提高这个呃效率啊。</p> <p>而接下来l『『我们要学习的这个linux系统io函数呢，他们是没有这个缓冲区的啊』』，我们调用一次这个比如说呃red啊。
那么他就会写一次这个磁盘啊那调用一次这个read，他就会去读一次这个磁盘。
啊那这儿呢大家要啊了解啊，那了解了以后呢，我们就可以在合适的这个时机啊选择合适的这个函数啊。
那就比如说我刚才说的这个网络通信，对吧？</p> <p>所以说呢在这个网络通信的时候呢，我们啊就应该使用这个呃linux系统的io函数啊，
那么在我们呃对这个磁盘进行读写的时候呢，哎那我们就需要有这个。
缓冲区，对吧？
提高我们这个效率啊就应该选什么选这个啊标准c库的含有函数，那这点呢大家要搞清楚。</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>![02](./img/1.17/02.png)
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br></div></div><hr> <ul><li>标准C库的IO是带缓冲区的，提高了效率：指的是，降低我们操作这个磁盘的效率。</li> <li>而我们要学的Linux的『系统IO函数』呢：<strong>他们是没有缓冲区的</strong>！！</li> <li>我们调用一次这个，比如说write，它 就会写一次这个磁盘！</li> <li>啊那这儿呢大家要啊了解啊，<strong>那了解了以后呢，我们就可以在合适的这个时机啊选择合适的这个函数</strong>啊。</li> <li>那就比如说我刚才说的这个网络通信，对吧？<strong>那网络通信呢它肯定是要要求这个呃效率的</strong>，你想想是不是啊？你跟这个别人进行一个网络通信肯定是要求这个效率的，对吧？</li> <li><strong>那比如说呃你想想我给你发了这个十句话，如果说这个十句话还在这个缓冲区里面，那你说你着不着急啊</strong>？</li> <li>对吧？肯定着急嘛是吧？那如果这个数据啊在缓冲区里面，说明这个数据还没有发出去，是吧？</li> <li>所以说呢在这个网络通信的时候呢，我们啊就应该使用这个linux系统的io函数啊，</li> <li>那么在我们呃对这个磁盘进行读写的时候呢，哎那我们就需要有这个缓冲区，对吧？提高我们这个效率。</li> <li>啊就应该选什么选这个啊标准c库的IO函数，那这点呢大家要搞清楚。</li></ul> <hr> <p>好了，那这节课呢我们就先介绍到这儿啊，我们下一节课呢再见。</p> <div class="language-txt line-numbers-mode"><pre class="language-text"><code>缓冲文件系统
缓冲文件系统的特点是：在内存开辟一个“缓冲区”，为程序中的每一个文件使用，
- 当执行读文件的操作时，从磁盘文件将数据先读入内存“缓冲区”，装满后再从内存“缓冲区”依此读入接收的变量。
- 执行写文件的操作时，先将数据写入内存“缓冲区”，待内存“缓冲区”装满后再写入文件。

由此可以看出，内存 “缓冲区”的大小，影响着实际操作外存的次数，内存“缓冲区”越大，则操作外存的次数就少，执行速度就快、效率高。一般来说，文件“缓冲区”的大小随机器而定。
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br></div></div><div class="language-png line-numbers-mode"><pre class="language-text"><code>&lt;img src=&quot;./img/1.17文件IO/01.标准C库IO函数.JPG &quot;&gt;               
&lt;img src=&quot;./img/1.17文件IO/02.标准C库IO和Linux系统IO的关系.JPG&quot;&gt;  
&lt;img src=&quot;./img/1.17文件IO/03.虚拟地址空间.JPG&quot;&gt;                  
&lt;img src=&quot;./img/1.17文件IO/04.文件描述符.JPG&quot;&gt;                    
&lt;img src=&quot;./img/1.17文件IO/05.Linux系统IO函数.JPG&quot;&gt;               
&lt;img src=&quot;./img/1.17文件IO/06.stat结构体.JPG&quot;&gt;                 
&lt;img src=&quot;./img/1.17文件IO/07.st_mode变量.JPG&quot;&gt;
&lt;img src=&quot;./img/1.17文件IO/08.文件属性操作函数.JPG&quot;&gt;
&lt;img src=&quot;./img/1.17文件IO/09.目录操作函数.JPG&quot;&gt;
&lt;img src=&quot;./img/1.17文件IO/10.目录遍历函数.JPG&quot;&gt;
&lt;img src=&quot;./img/1.17文件IO/11.dirent结构体和d_type.JPG&quot;&gt;
&lt;img src=&quot;./img/1.17文件IO/12.dup和dup2函数.JPG&quot;&gt;
&lt;img src=&quot;./img/1.17文件IO/13.fcntl函数.JPG&quot;&gt;
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br></div></div><h2 id="_18-虚拟地址空间"><a href="#_18-虚拟地址空间" class="header-anchor">#</a> 18-虚拟地址空间</h2> <p>嗯。好了，那上节课呢主要给大家去介绍一下标准c库io 函数。
和linux 系统i o 函数的区别啊，那我们再介绍标准c q y o 函数的时候呢，我们说啊这些标准c库i o 函数啊。
他是如何去操作磁盘当中的文件呢？啊，其实就是通过一个叫file 的文件指针去进行一个操作，对吧？那你比如说呢我们。通过这个fopen去打开一个磁盘当中的文件。那么这个函数呢它会返回一个这个返回值啊，这个返回值呢就是一个file 类型的文件指征。
啊，那么通过这个文件指针呢，我们就可以去操作</p> <ul><li>磁盘当中的文件。那你比如说啊我们在后面呢调用这个fwrite 啊。
那么这个函数包括这个fr 的对吧，等等这些函数里面是不是都需要传递一个这个file 新的文件指针啊，对吧？那这些函数呢其实就是通过这个文件指征。操作磁盘当中的这个文件啊，那么在这样呢我们呃还需要知道的就是这个file 文件指针当中啊，他其实就是一个结构体，对吧？</li></ul> <blockquote><p>感觉是复习</p></blockquote> <ul><li><p>那它里面呢有几个比较重要的部分啊，我们需要知道的一个呢是这个文件描述符啊，那他呢是一个整形值，它是用来定位我们这个文件用的。那这个文件描述符呢，它会指向啊我们需要操作的这个啊磁盘文件啊。那么还有一个呢是这个文件读写指针。那他呢是啊，对我们这个文件当中的这个数据进行一个操作的，对吧？通过这个文件啊读写指针去进行一个操作啊，那它里面呢有这个。读指针还有这个写指针，对吧？然后呢还有一块呢就是这个缓冲区啊，那这个缓冲区呢它主要是用来提高这个效率的，对不对？</p></li> <li><p>那么其实呢我们也可以<code>通过这个代码</code>去看一<code>看这个file 文件指针</code>啊。那在这呢我们打开v s q 的啊，然后呢我们在这里面呢。</p></li> <li><p>随便呢去新建一个啊test 点c 啊，随便新建一个这个文件哈。
那么在这样呢，我们呃。
先引入一个s d。
啊，i o 点h 然后呢写一个泛函数。
不测零。
好，然后呢我们直接啊去输入这个file。
啊，全部都是大写，对吧？好，那么它是一个结构体啊，我们就直接写一下，然后按住肯鞘腱鼠标呢，你上来变成一个小手，对吧？点进去。
好，那么我们来看一下它是在哪呀？他是在这个，你看是不是在叉八六杠六十四linux gnu 啊beats。
taes, 然后里面呢有一个头文件叫yr 点h 是吧？也就是说它是定义在这个三号点h 头文件里面的啊，那我们来看一下。
在这里面呢，他只做了一件事情啊，就是重新给他起了一个这个定义了一个类型，对吧？啊，就是呃file 这是新的类型，然后。
男生的类型呢叫下划线i o 下划线画哦，然后呢它是一个r 的结构体啊，然后我们去看一下这个真正的啊这个file。
好，那它是一个什么呀，结构体吧，对不对？那么在这里面呢，我们看一下它里面的内容啊，比如说照呢有一个岔心对吧？这些都是查新，都是指针吧。
有什么呢？有read p t r，read and read base, 还有right base，right pd 啊啊right end 啊，还有这个。
八核base buffer end 啊等等，对吧？那么这个read p t r 呢其实就是我们之前所说的这个读指针啊，right p d r 呢就是。
啊，斜指着啊，然后这个八五base 呢，其实就是我们这个缓冲区啊，它的一个起始啊，及时的一个指针起始的位置。然后这个buff n 的呢。
是这个缓冲区的一个结束的一个位置啊，我们说这个file 结构体里面对吧？这个文件指针里面它是不是有一个缓冲区啊，对吧？最后缓冲区呢啊他其实就是通过这个指针来进行一个维护的。
有一个起始的，有一个结束的对吧？好，那这个是啊，我们说有缓冲区，还有这个读写指针，对吧？然后我们再往下看啊，还有一个什么呢？
还有一个叫in 的类型的file number 啊，这个方向number 呢其实就是啊我们之前说的这个文件描述符啊文件描述符。
那么这个文件描述符它是怎么来的呢？
这个文件描述符啊，其实啊我们说这个。
标准西裤的io 函数啊，他在调用的时候是不是首先会去调用我们这个啊linux 系统的i o 函数啊，对吧？那。
系统的i o 函数呢它会返回啊，我们后面会看到啊，它会返回一个这个文件描述符啊，就是这个fd 啊它的一个这个标识。
啊，那然后呢，我们这个标准西裤的啊i o 函数呢得到了这个文件描述符以后呢，唉再给这个i number 进行一个赋值，对吧？
附上你这个啊系统调用啊，就是这个linux 系统调用返回的那个文件描述符的值复制给他啊。所以说呢，我们说啊这个标准。西裤。
啊，io 当中啊，它都是通过发小新啊，就是一个文件指征，对于文件呢进行一个操作对吧？那么他怎么去找到这个啊文件，对吧？
是通过这个啊fd 文件描述符啊，它的一个这个值啊，去找到我们对应的这个文件定位。我们文件啊。
那怎么操作呢？就是通过这些啊读指针写指针啊，还有这个缓冲区对我们的数据呢进行一个操作啊。好，那这个呢大家也要知道一下。
好，那么接下来呢我们给大家去介绍第二个概念。
啊，那么这个概念呢叫做。
虚拟地址空间。
啊，虚拟地址空间。
那咱们呢先把第二个概念啊介绍完了以后啊，我再给大家去介绍这个文件描述符。
那么这个虚拟地址空间啊，首先问大家啊，这个虚拟地址空间它存不存在？
他存不存在呢啊。
那比如说我说啊呃有一个虚拟的世界，那你说这个世界它存不存在了，呃，虚拟的世界肯定是不存在的嘛，对吧？那所以说呢这个虚拟地址空间了。
他也是不存在的啊，那他呢是我们程序员啊想象出来的啊想象出来的。那么这个虚拟地址空间啊。
他是干嘛用的啊，那在这样呢呃简单的给大家去说一下啊啊好，让大家呢去呃理解啊，为什么要有这个虚拟地址空间。
啊，那举个例子啊，比如说我们啊有一个电脑对吧？那他呢有这个四g 的内存。
啊，比如说我画四个格子，一个格子呢就是呃一个g 对吧？那比如说我有一些应用程序啊，就是说我有三个啊，第一个应用程序呢占一g。
啊，第二个应用程序啊，它应用这个启动起来以的话占两g 啊，然后呢还有一个呢是蘸这个两g。
那比如说啊我现在呢有一个程序一g 的应用程序启动起来了，是不是占了一寄了。然后第二个它是不是占了两g 是不是呃占了中间这个。
两g 对吧？好，然后呢，你看现在是不是总共是四g 嘛？
现在已经占了三g 了，对吧？那我还要一个应用程序是占两g 它能够运行吗？他不能运行，因为你只剩遗迹了，对吧？啊，如果说。
按照我们这个正常的想法，它是这样去运行的嘛，对吧？加载到内存里面嘛。好，那么现在这是一个问题，就是说我总共就四g 的内存啊。
你现在有这么几个应用程序啊，他要占用的内存呢都呃比较大，对吧？一g 两g 两g 那肯定是不能都给它加载进来。那现在有个问题啊。
这是第一个问题啊，那么接下来再来看，比如说我一g 的内存啊，这个释放了，是不是现在我有呃两g 的空余啦，就是说呃前面一句和后面一级对吧？
中间的两g 是不是还是占用着呢？那么这个时候我另外一个两g 的应用程序能不能加载进来呢？
能肯定是能对吧？但是有个问题，什么问题呢？就是说你现在加载的话，你是不是呃前面这个加在一起，后面再加载一件，那它的这个内存地址是不是就断掉了？
对吧就断掉了啊。好，那么这是我们刚才说的啊两个问题。其实还有一些其他的问题啊啊那么你你想一想，如果说我们用这种方式去。
啊，加载这个数据的话，加载啊程序到内存里面的话，是不是有这么一些问题啊，对吧？好，那为了解决这个问题呢啊其实这个啊一些这个研究人员啊。
啊，他就发明出了什么呢？哎，虚拟地址空间啊，就是虚拟内存的这么一技术啊，那么虚拟内存呢其实就是它会有一个虚拟地址空间。
那么这个虚拟地址空间呢，它是不存在的啊不存在的那它是怎么得到这个虚拟地址空间呢？
那么首先哈呃咱们呢要有一个可执行程序啊，我们要把这个可执行程序啊运行起来啊。那么在这个可执行程序运行期间了。
啊，那么其实他就会对应一个虚拟地址空间啊，那么如果说这个程序啊运行结束了啊，那么这个虚拟地址空间它就不存在了。
啊，就不存在了，明白吧？那么其实这个程序啊运行起来之后啊，它其实就是一个进。
啊，经常呢我们后面要啊给大家去介绍的啊，那么呃其实进展呢就是啊我们系统啊给这个程序分配资源的一个这个最小单位。
啊，就是我们程序要运行嘛，对吧？那我们系统呢就啊创建一个进程啊，给他分配一些资源，对吧？让我们这个程序呢去运行啊，这个就是进程啊。
那么其实这个地方呢大家要搞清楚啊这个进程啊和程序的一个区别。那所谓程序呢它是什么呀？所以程序它就是啊我们这个磁盘上的代码。
他只是在这个磁盘上啊，那么进程或者说运行中的程序啊，他会加载到内存里面对吧？那么这个呢是叫做进程，它是呃我们运行这个这个程序。
你看吧啊，这个两个不一样。
那么这个虚拟地址空间了啊，他其实就可以用来解决啊我刚才所说的啊，就是我们这个把这个应用程序啊加载到内存里面。
啊，可能会产生的一些问题，对吧？可能会呃有一些的这个问题啊，那么当然了我们重点呢不是说去研究啊这个虚拟啊、内存啊。
就是他的一个技术是怎么实现的啊，咱们呢只需要理解这个虚拟地址空间里面的一部分内容就ok 了，明白吧？
那理解这部分内容呢，主要是为了我们后面呢啊学习。比如说这个进程对吧？比如我创建了一个这个进程，我又创建了一个这个紫禁城啊，那么它里面的这个啊内存啊。
啊，是如何变化的，对吧？啊，比如说这个占空间啊或者说非空间它会被复制啊，对吧？是为了让我们去解释这些东西的啊，理解我们这个应用程序。
他执行的一些啊东西的，明白吧？所以说在这样呢，大家也不要去觉得啊这个虚拟地址空间啊啊不重要，或者说呢啊比较难学啊，我们所需要了解的东西呢。
啊，非常少啊。好，那呃回到这个虚拟地址空间啊，我们继续来说啊，我刚才说呢这个一个应用程序啊运行起来以后呢，它就对应了一个进程，对吧？
那么这个进程呢其实就会有一个虚拟地址空间啊是虚拟出来的，明白吧？那么这个虚拟地址空间它有多大呢？那这个呢是由电脑的c p u 啊来决定的。
啊，那比如说这个三十二位的计算机啊，那他的这个呃虚拟地址空间的大小呢就是二的三十二次方啊。那么大概呢就是四个g。
啊，那么六十四g 的这个六十四位的机器呢啊可能同学会这样去想啊，就是二的六十四次方啊，其实不是啊，一般呢他是这个二的四十八次方。
啊，那这个呢就非常大了。
那样吧。那么在这呢，我们就以这个三十二位的机器来说了，明白吧？我们不以这个六十四位的去说啊，我们就以三十二位的这个机器来理解啊。
那么所以说呢，你要知道有这么一个模型啊，就是进程啊运行起来以后啊，他需要对应这样的一个模型来解释一些东西啊，就是什么呢？
比如说同学妈妈都听过的这个站，对吧？哎，对对不对等等啊，它是用来解释这些内容的啊。好，那咱们来看一下这个虚拟地址空间啊。
那么这个虚拟地址空间呢，它分为了两部分啊，一部分呢是这个零到三g 啊，我们说三十二位的机器嘛，对吧？它是这个虚拟地址，空间呢是四g 啊。
然后一个是零到三g 一个是三g 到四g 这个里面是一g 对吧？然后下面是三g 啊，那么零到三g 呢它是这个用户去。
啊，用户去，然后。
这个还有一件呢是这个内核区啊内核区。那么呃这个虚拟地址空间里面的这个数据啊，他会被什么？他其实最终啊会被我们的。
这个cpu 当中的一个啊这个逻辑单元啊，逻辑管理单元叫做这个mm u 啊，m u 就是内存管理单元啊，比这个东西。
然后呢，被他干嘛呢？映射到我们的真实的一个物理内存上看看吧，就是说这个虚拟地址空间是为了方便我们啊，一个是解决我们刚才说的那些问题。
对吧啊就是这个内存加载的一些问题啊，一个呢是解决我们说这个啊这个站啊堆啊啊解释这些概念呢，对吧？解释这些模型的。
好好，那我们说啊这个虚拟地址空间啊，它最终呢会被啊我们这个c p u 里面的一个逻辑管理单元叫做mm u。
啊，m m u.
给他映射到我们这个内存啊，真实的物理内存上，对吧？那么呃大家想一想这个mm u 啊就是这个c p u 里面的一个这个。
程序员对吧？或者说一个管理东西啊管理器它是不是就应该啊能够将我们这个虚拟地址啊和我们这个物理地址之间进行一个转换。
对吧啊就是我们所说的这个虚拟内存管理啊这一个技术啊，那么我们一般说的时候呢，我们不是拿这个呃真实的物理内存来说了啊，我们一般呢都是拿这个虚拟地址空间去说。
啊，就是你知道啊这个虚拟地址空间里面的数据啊，它最终呢会全部映射到实际的物理内存当中啊，它被水印什么就是被这个cpu 去映射。
啊，那么呃当然了，我们说这个虚拟内存地址啊，就是虚拟地址空间，它占四g 对吧？四g 的空间啊，那这个呢只是一个理想的状态。
啊，它最多呢只能加自己啊，实际上呢他占用这个物理内存有四g 嘛啊其实不会啊，实际上它是不会占用这个啊物理内存四g 的，明白吧？
啊，那这个呢都是由这个cpu 去管理的啊，咱们呢不用去啊重点的关注啊。
那么咱们再来看这两个区啊啊首先呢说这个零到三g 它呢是用户区啊，那用户区呢就是啊说可以让这个用户啊去自己操作。
好啊，那咱们呢作为一个普通的用户啊，你可以操作里面的这个啊东西啊，比如说我们运行一个应用程序啊，那么就可以操作这个用户区里面的某些空间。
啊，你比如说这个堆空间。
对吧啊你比如说这个占空间是吧，你都可以去进行一个操作啊。
然后呢，再往上的话呢，就是这个呃内核了啊，那么内核呢它是啊我们这个普通用户啊是不能进行一个操作的。
啊，我们是没有这个权限去操作的啊，那我们来看一下这个用户区域啊有哪些东西啊。啊第一个呢比如说受保护的地址啊，一般呢是这个零到四k。
啊，那像我们这个now。
那大家都知道空指针对吧？还有这个number one point inter。
啊，那我pt 啊，那么像这些呢，其实他都是在这个地址里面啊，受保护的地址啊。然后呢，还有像这个。
text 字段了，这个呢是代码段啊，所谓代码的呢就是我们程序啊是不是要加载到内存当中啊，那加载的话是加载的这个代码啊代码的这个数据。
啊，都是这些二进制的机器指令啊，然后呢像这个date 对吧？它里面呢是以初始化的全局变量啊，l o b s s 段。
它里面呢是未初始化的全局变量啊，比如说我们呃这个定义了一个全局变量啊，然后没有初始化，那么他会放在这个里面啊，包括你直接呢付一个零，它也是没有初始化，也是放在这个里面了。
啊，然后这个堆对吧？堆空间啊对于大家都知道吧，我们可以通过这个new 或者mallock 创建出来是吧？创新出来的这个数据都是在堆里面进行管理的。
啊，然后还有这个共享库啊，那共享库呢其实就是啊加载我们之前所学习的这个啊共享库，它里面的这个代码在在在这个里面啊，然后还有这个。
占空间啊，那么一般的这个堆空间呢，它是要比占空间要大的，对吧？这个我们也知道啊，占空间呢他一般呢啊都比较小是吧？好，然后呢大家再来看这个箭头啊。
啊，对，空间呢它是从下往上，也就是说从dd 值往高地址去存啊，就是说你有数据的话，它是先存帐，然后再往上去存的。
而这个占空间呢，它是从这个上往下去层啊从上往下去走，就是说高地址往低脂低地址去测是吧？那么占空间里面是不是像这些。
不变量啊，对吧？都是保存在栈空间里面啊。
然后呢，还有这个命令行参数，这个大家应该都好理解是吧？命令行参数我们这个是不是呃比如说我们执行这个程序是不是可以传递这个参数进来。
对吧，这个就是命令行参数放在这里面，还有一些这个环境变量啊啊我们这个每个用户啊，linux 每个用户啊其实都会有一些这个环境赔偿。比如说我们输入nv。
啊，那么他会把这些环境变量的这个数据啊放到哪呢？放到这个啊这一块里面。
明白吧，啊，好，那这是用户区里面的一些内容啊。当然了，这个用户区里面呢还有一些其他的内容啊，我们呢只需要关心这几个啊比较常见的就ok 了。
啊，然后呢再来说这个linux 这个内核区啊，那我刚才说到这个内核区里面的这个数据啊，我们是没有权限去操作的。
普通用户是没有权限去操作的啊，那么内核区的数据啊对于咱们一个普通用户来说啊，既没有这个读权限，也没有写权限。
啊，那如果说我们想要操作这个内核里面的这个数据，我们应该怎么办呢？那我们啊就必须要进行这个系统调用啊，那所谓的系统调用呢就是。
啊，调用linux 系统的api 就是调用linux 系统的api 这个叫系统调用，明白吧？我们可以通过这个系统调用。
通过系统api 函数来完成某些操作。啊，你比如说啊我们linux 里面有这个read 这个io。
法律的rise 是吧？我们可以通过这个系统调用啊，对于我们这个内核里面的数据啊去进行一个操作。比如说呢我们通过read 呢去读取。
当中的这个数据对吧？或者说呢通过write 呢去写数据啊。
那么内核区他都有有些啥呢？啊，比如说呢这个内核区呢它可以进行一个内存管理啊，还可以进行进程管理，对吧？还可以。
啊，像这个设备驱动管理啊，所谓设备驱动管理呢，比如说这个网卡驱动对吧？显卡驱动啊也是在这个内核当中进行一个管理的。好，然后呢还有这个。
啊，o v f s 虚拟文件系统对吧？那每一个这个操作系统是不是都会有文件系统啊，对吧？linux 它也会有啊。
好，那么到这呢啊就给大家呢介绍了这个虚拟地址空间啊，那咱们呢总结一下啊，那首先呢说这个虚拟地址空间啊。
他是不存在的，对吧？他只是为了让我们去解决这个啊程序啊，加载内存啊啊他的一些问题啊，以及呢。
啊，解释一些我们这个编程过程当中啊所需要用到的这些模型。比如说站啊，对呀对吧？它是用来做这样的事情的啊，然后呢零到三g 呢使用户区。
啊，我们可以直接对它进行操作，那么三次三g 到四g 呢是内核区，我们是不能直接去操作的。那我们要想去呃访问里面的数据啊，我们应该通过这个。
调用系统api 对吧？就是执行这个系统调用去进行一个操作啊。好，那么呃到账呢就给大家介绍了这个虚拟地址空间啊，我们后续呢。
还会再去给大家介绍这个虚拟地址空间里面的一些其他的内容啊。那这节课呢我们就先介绍到这啊，我们下一节课呢再见。</p></li></ul> <h2 id="_19-文件描述符"><a href="#_19-文件描述符" class="header-anchor">#</a> 19-文件描述符</h2> <p>他。
好了，那上一节课呢给大家去介绍了一下虚拟地址空间。
啊，那掌握了这个内容之后呢，我们这节课去学习一下文件描述符。那么在这呢首先啊去打开我这个sl 啊。
那么在当前这个目录下面呢，比如说有一个test 点c 啊这样的一个原文件，对吧？那我们啊可以通过gcc 啊对于这个猜测点击呢。
进行一个编译啊，编译之后呢生成一个可执行文件。
那么比如说呢我们生成的这个可执行程序啊是这个test，对吧？那么呃在这呢我们再去给大家去说一下这个程序和进程啊他们的一个区别。
那你比如说呢我们写的这个test 点c 啊，它其实就是一个程序，对吧？它是程序的源代码啊，那么程序呢它是呃不占用这个内存空间的。
啊，他只占用这个磁盘的空间啊，它是稳健。那再比如说这个test，这个是可执行程序，对吧？那他也是一个稳健啊可执行文件。
那他呢也是只占用我们磁盘的空间，它不占用内存的这个空间啊。
那么什么是进程呢？就是当我们程序啊要运行起来的时候啊，比如说这个test 这个可执行程序，对吧？我们要把它给启动起来啊，那么你要去运行一个程序啊。
那操作系统呢应该会为这个程序呢啊这个它的一个启动对吧？或者说他的运行分配一些资源啊，那这时候呢我们这个操作系统呢他就会去创建一个。
进程啊，那么这个呃进程呢其实就是啊给我们这个运行的程序，它所分配的一些资源呢啊一个这个东西啊。
那么呃这个进程啊其实就是正在运行的程序嘛，对吧？那进程它是占用这个呃我们内存的一个空间的，对不对？
我们之前说啊啊这个一个进程启动以后啊，其实就是一个程序启动以后他会有一个虚拟地址空间，对吧？那么这个虚拟地址空间呢他会通过这个cpu 当中的啊mu 这个罗。
级单元啊逻辑管理端妍把我们的这个虚拟地址空间当中的数据映射到哪儿去，映射到这个啊物理内存当中去。
对吧啊那么这个呢是啊这个程序啊和这个进程他们之间的一个区别啊，大家呢一定要搞清楚。
那么比如说呢我现在呢通过这个test 点c 啊生成了一个test 啊这样的一个可执行程序啊可执行文件。那么在这个test 这个可执行文件里面呢。
我们比如说呢做了一个读文件的操作啊啊，或者说呢做了一个这个啊写文件的操作啊。比如说呢我在这儿呢去创建一个。
个比如说。
a a 点或者说english。
啊，点txt。
可以吧。
啊，有一个english 点txt 啊，它是里面呢有一些数据。那比如说呢我们这个test 这个可行性程序啊运行起来以后啊，那么操作系统呢会呃生成一个进程。
啊，比如说这个进程呢叫做a 啊a 进程。那么这个进程呢唉他就会去读取或者说呢去啊写这个数据往哪去写呢？往这个english。
txt 里面去写啊，或者呢从这个里面去读取数据，对吧？那么我们啊如果说用这个标准c 啊，它的一个i o 函数库。
我们应该怎么去操作，所以首先呢fo come 对吧？di opp 打开这个文件啊，然后呢调用f read 去读取数据或者呢fright 去写数据。
对不对？
那么现在呢啊问大家一个问题，就是说呃为什么在这个程序当中啊，我们通过fo 碰以后啊，调用f read 或者fright，他就能够去找。
找到磁盘上的这个english 点txt 啊，对它进行操作呢。
啊，那这个我们之前说过是因为什么呀？因为它里面有一个文件描述符，对吧？我们说你调用这个open 啊ifo 本函数，它会返回一个啊file 文件指征。
这个文件指针里面呢，他就封装了一个啊这个内容啊，就是这个文件描述符它是用来去定位啊，你要操作的这个文件的，对吧？</p> <ul><li>那么这个文件描述符啊。他在什么地方呢？</li> <li>啊，注意了这个文件描述符呢，其实他是在我们的这个进程的内核区啊。</li> <li>好，那我们来看这个图啊。他其实呢是在我们这个进程的啊，它有一个这个内核区，对吧？我们说呃0-3G 呢是用户空间啊，用户区，然后三g 呢到四g 呢是这个内核区啊。</li> <li>那么这<strong>个文件描述符</strong>啊，它是保存在这个就是啊内核区的啊，你回去的有什么管理呢？<strong>有内核进行一个管理</strong>。啊，那么内核管理呢呃就是在这个进程管理这这一块啊，有一个呃PCB 啊，我们之前啊就是说这个内核区啊，它有一个内存管理，对吧？那其实就是在这个内存管理这个模块里面呢，哎它有一个模块叫做pcb 啊，pcb pcd 呢就是process control pro。
对吧啊进程控制块啊进程控制块啊，由他呢去管理这些啊，管理这个文件描述符。</li> <li>那么这个比较抽象啊，就是说你怎么理解呢？？？</li> <li>那你想啊呃，『『我们内核呢他其实就是一个程序，对不对？其实任何就是一个程序啊，我们所说的内核呢其实就是一个程序。那么内核他既然是一个程序啊，呃那比如说呢我们这个啊linux 内核它是由谁开发的，是不是有那个呃，李纳斯托瓦斯？</li> <li>他去开发的对吧？他用业余时间呢去写了一个linux 内核其实这个linux 那个就是一个程序，对吧？<strong>那为什么后来叫做这个操作系统呢</strong>？对吧？linux 操作系统的。</li> <li>『『其实就是说<strong>我们光有内核呢还不行</strong>！！！对吧？那操作系统它是什么样？<strong>操作系统呢，首先它能够对这个呃整个系统运行呢进行一个后台管理</strong>，对吧？那再有一个呢，<strong>你这个操作系统呢呃上面呢得有这个工具啊</strong>。比如说我们平常用的这个office，对吧？啊，比如说我们呃之前用过的这个gcc 啊编译器，对吧？啊，还有我们之前啊学过的这个GDB 啊调试工具。
不对，你得有这个工具啊。好，那么这样呢才形成了一个这个啊这个操作系统啊，那么内核呢它其实就是一个程序啊。</li> <li>那呃那么<strong>这个PCB 啊其实就是一个啊非常复杂的结构体哈</strong>。那他是这个进程控制块啊。就是说PCB 呢它就是进程控制块啊，所以呢他要管理很多的这个东西，对吧？你想想我要去管理一个进程啊，那你是不是应该有一个东西啊去保存？？？啊，你需要管理的这些数据，对吧？啊，所以呢这个p d p 晋升这个进程控制块呢，其实就是啊一个非常复杂的结构体啊，<strong>那么我们可以把需要管理的数据啊都封装到了这个。</strong> <strong>pcd 啊这个『『结构体里面</strong>啊，那当然了，这个不是我们重点要研究的东西啊。那么你看到这个pcb 以后呢，你要知道它是什么就行了。</li> <li>啊，说的就是这个进程控制块。</li> <li>『『PCB这个结构体』』』那么<strong>在这个结构体里面呢，它其实有一个数组啊</strong>，那<strong>这个数组呢我们称之为什么呢？？？称之为文件描述符啊</strong>，我把这个放大。『『<strong>看PPT的右边</strong>』』</li> <li>那么在这个PCB 啊，这个呃你把它理解为是一个很复杂的一个这个结构体，对吧？那么在这个结构体里面呢，它有一个文件描述符表！！啊，文件描述符表。那我们<strong>说了这个文件描述符表呢，它是一个数组</strong>。</li> <li>啊，数组文件描述符表。那『『大家想一想，为什么要用这个数组来存储这个文件描述符呢？』』</li> <li>啊，<strong>文件描述符其实就是用来定位我们这个磁盘上的某一个文件的</strong>，对不对？那么在这个PCB里面啊，它有一个这个文件描述符表。</li> <li>是数组啊，那为什么他要用数组来存储这个很多个文件描述符呢？对吧？</li> <li>也就是说他是一个数组，它里面呢，可以存储n 个文件描述符啊，那每一个文件描述符呢都可以定位一个文件，是这样的吧，对不对？好，那为什么要有一个这样的一个表呢？啊，那你想啊<strong>我一个应用程序啊我一个应用程序启动了以后啊，那么你在这个应用程序里面啊，你比如说你打开了一个文件a。</strong>
是吧，那你能不能再打开其他的文件可以吧，对不对？那你比如说你打开了一个文件a 各位打开了一个文件b 又打开了一个文件c 啊，那你同时打开了这个十个。啊，这个文件是吧，那是不是就应该有十个啊，这个文件描述符分别对应或者分别定位到这十个文件？大家想一想是不是这样的，对不对？那么你一个进程你要管理这些数据啊，你是不是要有一个数组来存储这些文件描述符啊？对不对啊，所以说为什么在这个pcb 啊进程控制块里面，他要用一个数组来存储这些文件描述符啊，就<strong>是说我们一个进程呢它可以同时啊啊打开多个这个文件啊</strong>，所以所以说呢他要用一个数组，就是这个文件描述符表呢来存储啊。</li> <li>好，那现在呢大家应该能理解了这个啊文件描述符表为什么是一个数组。
对吧？</li> <li>好了，那么。
下来呢呃**再来说啊，这个文件描述符表就是这个数组啊，它的一个大小是多少呢？大小呢默认是1024啊，**默认的大小呢是1024就是说他的这个数组啊，这个默认大小呢是一零二四啊，那么你想一想每一个进程里边它是不是都有一个这样的进程控制块，对吧？啊，<strong>也就是说每个进程里面呢都有一个这样的文件描述符表啊，那你想每一个进程它默认能同时打开的文件的个数是多少，是不是就是1024啊，对吧</strong>？因为他这个。数组它的最大的一个大小啊，默认大小就是一零二四。所以说你最大能打开啊，同时能打开的这个文件的个数就是一零二四。</li></ul> <h4 id="『文件描述符』"><a href="#『文件描述符』" class="header-anchor">#</a> 『文件描述符』</h4> <ul><li>好，那这个呢大家要了解一下，然后呢<strong>我们再来看一下啊</strong>呃就是说呃<strong>这个文件描述符表当中啊，前三个他目瞪呢是被占用的，默认是被使用的</strong>。</li> <li>啊，那默认被谁使用呢？</li> <li>就是标准输出，</li> <li>标准输入，</li> <li>还有这个标准错误啊，标准输入标准输出还有标准错误。</li></ul> <blockquote><p>记忆：水管，开始in是0</p></blockquote> <ul><li>啊，其实就是我们之前啊学的这个s d d e 啊，s d dout, 还有这个s d d error，对吧？这几个啊标准输入输出，还有这个标准输出。一个错误啊，而且呢呃这几个这三个文件描述符表啊，就是这三个文件描述符啊默认是打开的一个状态。</li> <li>啊，他们默认是一个打开的状态啊，那呃。。</li> <li>那我<strong>问了这个标准输出标准输入还有这个标准错误，它都是和谁绑定的</strong>？都是和谁绑定的，</li> <li><strong>其实呢他们都对应了一个文件啊</strong>，那么他们对应的文件是谁呢？</li> <li>1、<strong>其实就是当前的终端</strong>，比如说我标准输出，他是不是输出到我们这个。啊，终端当中啊，对吧？啊，那么<strong>这个比较抽象啊</strong>，为什么比较抽象的就是说呃我们为什么啊这个文件描述符。啊，<strong>它指向的是当前终端啊，我们之前不是说这个文件描述符，它指向的是一个文件嘛，对吧</strong>？他定位的是这个文件啊。</li> <li>那你<strong>想一想啊，在这个linux 当中啊。他是不是有一句话叫做一切皆文件</strong>？对吧，一切皆文件。那么在linux 系统当中啊，<strong>不管是什么样的这个硬件设备啊，它最终呢都会虚拟成一个文件。通过这个文件呢去对它进行一个管理。</strong></li> <li>啊，那你比如说咱们的这个**显示器对吧？网卡啊、显卡都会被虚拟成这个啊文件啊。**那么通过这个文件呢去管理这些设备。啊，<strong>其实就是这个设备文件</strong>对不对啊，那么</li> <li>『『这个<strong>标准输出标准输入，还有这个啊标准错误，其实呢就是指向了当前的这个终端</strong>啊。他<strong>指向的是一个设备文件</strong>啊，』</li> <li>那这三个大家一定要搞清楚啊啊他们分别呢是零一二啊啊这个他的红纸呢就是s d d e f a number。s d dout 发扬number，还有这个s d da l 发扬number。这三个呢大家要记好了，他默认是打开的，对应于我们这个当前的一个终端啊。那么呃<strong>这个标准输入啊输出错误啊，刚才也说了，他们分别对应的这个值啊是。0,1,2</strong>，零一二啊<strong>就是呃固定的啊不可能发生改变的，明白吧</strong>，啊，那么呃<strong>我们啊通过这个0，1,3呢就能够找到他所绑定的一个啊设备终端。</strong></li> <li>啊，<strong>还有呢就是这个0,1,2呢，他们对应的终端呢是同一个！！就是我当前使用的这个终端啊</strong>，比如说呢我们在这个呃地方啊。</li> <li>『『『比如说我们去运行这个test，对吧？那呃他的这个标准输入输出，还有这个标准错误啊，就对于我当前这个终端，<strong>那我再打开一个，是不是就是对应另外一个这个终端啊。</strong>『HACV，觉得有理由，可以考虑，<strong>我每次点击一个程序，就弹出一个黑色的框框，2333，这下真相</strong>了。。』</li></ul> <h4 id="『同一文件，不同文件描述符』"><a href="#『同一文件，不同文件描述符』" class="header-anchor">#</a> 『同一文件，不同文件描述符』</h4> <ul><li>对不对啊？好，那再回过来看啊。那么呃我刚才说了，这三个文件描述符对应的是同一个终端是吧，那<strong>也就是说啊这个不同的文件描述符它可以对应啊同一个文件啊</strong>，也就是说啊什么意思呢？比如说我一个文件啊，比如说a 点txt。我可不可以调用这个fopen 放函数去打开一次可以吧**。那我能不能再调用一次open o ifo 版函数，再去打开也可以**，对吧？好，那我还可以再调一次。再去打开，我可以调用多次。那么<strong>你多次调用的话，这个fopen返回的这个FILE*啊，就是一个file 文件指针它里面。</strong> <strong>这个指向的这个文件描述符就是定位的文件描述符</strong>，它的值一不一样的啊，**注意了是不一样的是不一样的啊，**明白吧？所以呢我们这个一个文件呢可以同时被打开多次。那么打开多次呢他。</li> <li>这个文件描述符呢是不一样的。比如说我第一个呢打开我往里面去写数据。第二个打开以后呢，我往里面这个从里面读数据是不是啊？</li> <li></li> <li>『『那么我们<strong>什么时候这个呃，这个文件描述符啊释放掉呢</strong>，就是我们去<strong>使用这个啊fclose 或者说呢我们后面使用这个linux他的一个啊系统i o 函数啊叫close 啊，去关闭这个文件描述符的时候呢</strong>，』』这个文件描述符呢他就会被释放啊，</li> <li><strong>那被释放了以后呢。是不是就可以重新去使用了，对吧</strong>？可以重用这个文件描述服务了啊。那么<strong>如果说文件描述符啊他被占用的话呢，哎那它需要到这个文件描述符里面。就是表里面去找啊，找什么呢？找一个最小的啊，没有被占用的这个文件描述符去使用啊</strong>。</li> <li>那么可能有的同学呢会有疑问，就是说呃。<strong>那我怎么知道这个呃，找最小的这个文件描述符，对不对啊</strong>，那这个呢<strong>我们不需要去关心为什么呢？因为这个是由内核来维护的</strong>。对不对啊，这个是由内核来维护的啊，我只是给你描述一下这个内核的行为啊。举个例子，比如说呢。我们啊呃现在呢前三个这个文件描述符啊是不是都被占用了，对吧？零一二啊，那比如说呢我再去打开一个文件，a 点txt。
那么这个时候他会找最小的没有被占用的。三是不是给你返回啊，对吧？啊，然后呢，比如说我又打开了一个b 点t s t。
那么他会找谁？他会找最小的没有被占用的四啊，指向这个文件啊。然后呢，比如说我又打开一个a 点t s t，那你又打开了同一个这个文件。
他是不是啊，同样的还会再去返回一个新的文件描述符，对不对啊，指向他啊，然后等等等，你可以往下啊一次去使用。
那么现在如果说我这个a 点tfd 第一个啊打开的我释放掉了，就这个文件描述符表，就是这个文件描述符三啊，我释放掉了。释放讲了以后是不是就可以。
重用呀，对吧？好，那比如说我下面再打一个一个这个c 点tsc，那么他应该使用的是什么？使用的是最小的，没有被占用的，是不是三啊，对不对啊？
么这个是这个内核把我们去完成的啊，内核的行为就是这样的啊。好了，那么关于这个文件描述符呢啊咱们理解那么多就ok 了。</li></ul> <h4 id="『获得文件描述符』"><a href="#『获得文件描述符』" class="header-anchor">#</a> 『获得文件描述符』</h4> <ul><li>啊，<strong>那么我们怎么去得到一个文件描述符呢？啊</strong>，那我们<strong>后面呢啊要给大家去介绍这个linux 的啊系统api 啊</strong>，系统i o 的这个api 啊。
这个<code>open</code>。
<strong>好，<code>open</code> 这个系统函数。那么通过他呢就可以去得到一个文件描述符</strong>啊，那么得**到了这个文件描述符有什么用呢？我们是不是就可以操作这个文件描述符？**所对应的那个文件对吧？</li> <li>也就是说我们后面呢，比如说我们要去读数据啊，用的这个read 啊，或者写数据用的这个write 啊，那么这个里面呢呃我们在调用的时候呢，就不是传递啊。这个像啊标准c 库的这个啊IO 对吧？它的一个api 了传递一个什么样，传递一个话哦，这个指针是吧？那现在呢我们呃调用这个linux 的。啊，系统的这个api 呢，我们传入的就是这个文件描述符的，通过这个文件描述符去操作啊，只指向的这个文件啊。好，那。
啊，这节课呢我们就介绍到这啊，我们下一节课呢再见。</li></ul> <h2 id="_20-open打开文件"><a href="#_20-open打开文件" class="header-anchor">#</a> 20-open打开文件</h2> <blockquote><p>Linux的『系统IO』函数</p></blockquote> <h4 id="open-c"><a href="#open-c" class="header-anchor">#</a> open.c</h4> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">/*
    #include &lt;sys/types.h&gt;
    #include &lt;sys/stat.h&gt;
    #include &lt;fcntl.h&gt;

    // 打开一个已经存在的文件
    int open(const char *pathname, int flags);
        参数：
            - pathname：要打开的文件路径
            - flags：对文件的操作权限设置还有其他的设置
              O_RDONLY,  O_WRONLY,  O_RDWR  这三个设置是互斥的
        返回值：返回一个新的文件描述符，如果调用失败，返回-1

    errno：属于Linux系统函数库，库里面的一个全局变量，记录的是最近的错误号。

    #include &lt;stdio.h&gt;
    void perror(const char *s);作用：打印errno对应的错误描述
        s参数：用户描述，比如hello,最终输出的内容是  hello:xxx(实际的错误描述)
    

    // 创建一个新的文件
    int open(const char *pathname, int flags, mode_t mode);
*/</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/stat.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fcntl.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 打开一个文件</span>
    <span class="token keyword">int</span> fd <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;a.txt&quot;</span><span class="token punctuation">,</span> O_RDONLY<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>fd <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;open&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    <span class="token comment">// 读写操作</span>

    <span class="token comment">// 关闭</span>
    <span class="token function">close</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br></div></div><h2 id="_21-open创建新文件"><a href="#_21-open创建新文件" class="header-anchor">#</a> 21-open创建新文件</h2> <h4 id="create-c"><a href="#create-c" class="header-anchor">#</a> create.c</h4> <div class="language-cpp line-numbers-mode"><pre class="language-cpp"><code><span class="token comment">/*
    #include &lt;sys/types.h&gt;
    #include &lt;sys/stat.h&gt;
    #include &lt;fcntl.h&gt;

    int open(const char *pathname, int flags, mode_t mode);
        参数：
            - pathname：要创建的文件的路径
            - flags：对文件的操作权限和其他的设置
                - 必选项：O_RDONLY,  O_WRONLY, O_RDWR  这三个之间是互斥的
                - 可选项：O_CREAT 文件不存在，创建新文件
            - mode：八进制的数，表示创建出的新的文件的操作权限，比如：0775
            最终的权限是：mode &amp; ~umask
            0777   -&gt;   111111111
        &amp;   0775   -&gt;   111111101
        ----------------------------
                        111111101
        按位与：0和任何数都为0
        umask的作用就是抹去某些权限。

        flags参数是一个int类型的数据，占4个字节，32位。
        flags 32个位，每一位就是一个标志位。

*/</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/stat.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fcntl.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 创建一个新的文件</span>
    <span class="token keyword">int</span> fd <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;create.txt&quot;</span><span class="token punctuation">,</span> O_RDWR <span class="token operator">|</span> O_CREAT<span class="token punctuation">,</span> <span class="token number">0777</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>fd <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;open&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 关闭</span>
    <span class="token function">close</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br></div></div><h2 id="_22-read和write函数h"><a href="#_22-read和write函数h" class="header-anchor">#</a> 22.<code>read</code>和<code>write</code>函数h</h2> <h2 id="_23-lseek函数"><a href="#_23-lseek函数" class="header-anchor">#</a> 23.<code>lseek</code>函数</h2> <h4 id="lseek-c"><a href="#lseek-c" class="header-anchor">#</a> lseek.c</h4> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/*  
    标准C库的函数
    #include &lt;stdio.h&gt;
    int fseek(FILE *stream, long offset, int whence);

    Linux系统函数
    #include &lt;sys/types.h&gt;
    #include &lt;unistd.h&gt;
    off_t lseek(int fd, off_t offset, int whence);
        参数：
            - fd：文件描述符，通过open得到的，通过这个fd操作某个文件
            - offset：偏移量
            - whence:
                SEEK_SET
                    设置文件指针的偏移量
                SEEK_CUR
                    设置偏移量：当前位置 + 第二个参数offset的值
                SEEK_END
                    设置偏移量：文件大小 + 第二个参数offset的值
        返回值：返回文件指针的位置


    作用：
        1.移动文件指针到文件头
        lseek(fd, 0, SEEK_SET);

        2.获取当前文件指针的位置
        lseek(fd, 0, SEEK_CUR);

        3.获取文件长度
        lseek(fd, 0, SEEK_END);

        4.拓展文件的长度，当前文件10b, 110b, 增加了100个字节
        lseek(fd, 100, SEEK_END)
        注意：需要写一次数据

*/</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/stat.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fcntl.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">int</span> fd <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;hello.txt&quot;</span><span class="token punctuation">,</span> O_RDWR<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>fd <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;open&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 扩展文件的长度</span>
    <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">lseek</span><span class="token punctuation">(</span>fd<span class="token punctuation">,</span> <span class="token number">100</span><span class="token punctuation">,</span> <span class="token constant">SEEK_END</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>ret <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;lseek&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 写入一个空数据</span>
    <span class="token function">write</span><span class="token punctuation">(</span>fd<span class="token punctuation">,</span> <span class="token string">&quot; &quot;</span><span class="token punctuation">,</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 关闭文件</span>
    <span class="token function">close</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br></div></div><h4 id="hello-txt"><a href="#hello-txt" class="header-anchor">#</a> hello.txt</h4> <ul><li>见文件</li></ul> <h2 id="_24-stat和lstat函数"><a href="#_24-stat和lstat函数" class="header-anchor">#</a> 24.<code>stat</code>和<code>lstat</code>函数</h2> <h3 id="『1』stat-c"><a href="#『1』stat-c" class="header-anchor">#</a> 『1』stat.c</h3> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/*
    #include &lt;sys/types.h&gt;
    #include &lt;sys/stat.h&gt;
    #include &lt;unistd.h&gt;

    int stat(const char *pathname, struct stat *statbuf);
        作用：获取一个文件相关的一些信息
        参数:
            - pathname：操作的文件的路径
            - statbuf：结构体变量，传出参数，用于保存获取到的文件的信息
        返回值：
            成功：返回0
            失败：返回-1 设置errno

    int lstat(const char *pathname, struct stat *statbuf);
        参数:
            - pathname：操作的文件的路径
            - statbuf：结构体变量，传出参数，用于保存获取到的文件的信息
        返回值：
            成功：返回0
            失败：返回-1 设置errno

*/</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/stat.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">struct</span> <span class="token class-name">stat</span> statbuf<span class="token punctuation">;</span>

    <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">stat</span><span class="token punctuation">(</span><span class="token string">&quot;a.txt&quot;</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>statbuf<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>ret <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;stat&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;size: %ld\n&quot;</span><span class="token punctuation">,</span> statbuf<span class="token punctuation">.</span>st_size<span class="token punctuation">)</span><span class="token punctuation">;</span>


    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br></div></div><h2 id="_25-模拟实现ls-l命令"><a href="#_25-模拟实现ls-l命令" class="header-anchor">#</a> 25.模拟实现<code>ls -l</code>命令</h2> <h3 id="『1』ls-l-c"><a href="#『1』ls-l-c" class="header-anchor">#</a> 『1』ls-l.c</h3> <div class="language-c line-numbers-mode"><pre class="language-c"><code>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/stat.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;pwd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;grp.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;time.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string.h&gt;</span></span>

<span class="token comment">// 模拟实现 ls -l 指令</span>
<span class="token comment">// -rw-rw-r-- 1 nowcoder nowcoder 12 12月  3 15:48 a.txt</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 判断输入的参数是否正确</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>argc <span class="token operator">&lt;</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s filename\n&quot;</span><span class="token punctuation">,</span> argv<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 通过stat函数获取用户传入的文件的信息</span>
    <span class="token keyword">struct</span> <span class="token class-name">stat</span> st<span class="token punctuation">;</span>
    <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">stat</span><span class="token punctuation">(</span>argv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">,</span> <span class="token operator">&amp;</span>st<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>ret <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;stat&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 获取文件类型和文件权限</span>
    <span class="token keyword">char</span> perms<span class="token punctuation">[</span><span class="token number">11</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>   <span class="token comment">// 用于保存文件类型和文件权限的字符串</span>

    <span class="token keyword">switch</span><span class="token punctuation">(</span>st<span class="token punctuation">.</span>st_mode <span class="token operator">&amp;</span> S_IFMT<span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token keyword">case</span> S_IFLNK<span class="token operator">:</span>
            perms<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'l'</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> S_IFDIR<span class="token operator">:</span>
            perms<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'d'</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> S_IFREG<span class="token operator">:</span>
            perms<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'-'</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span> 
        <span class="token keyword">case</span> S_IFBLK<span class="token operator">:</span>
            perms<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'b'</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span> 
        <span class="token keyword">case</span> S_IFCHR<span class="token operator">:</span>
            perms<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'c'</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span> 
        <span class="token keyword">case</span> S_IFSOCK<span class="token operator">:</span>
            perms<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'s'</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">case</span> S_IFIFO<span class="token operator">:</span>
            perms<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'p'</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
        <span class="token keyword">default</span><span class="token operator">:</span>
            perms<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token string">'?'</span><span class="token punctuation">;</span>
            <span class="token keyword">break</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 判断文件的访问权限</span>

    <span class="token comment">// 文件所有者</span>
    perms<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>st<span class="token punctuation">.</span>st_mode <span class="token operator">&amp;</span> S_IRUSR<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">'r'</span> <span class="token operator">:</span> <span class="token string">'-'</span><span class="token punctuation">;</span>
    perms<span class="token punctuation">[</span><span class="token number">2</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>st<span class="token punctuation">.</span>st_mode <span class="token operator">&amp;</span> S_IWUSR<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">'w'</span> <span class="token operator">:</span> <span class="token string">'-'</span><span class="token punctuation">;</span>
    perms<span class="token punctuation">[</span><span class="token number">3</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>st<span class="token punctuation">.</span>st_mode <span class="token operator">&amp;</span> S_IXUSR<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">'x'</span> <span class="token operator">:</span> <span class="token string">'-'</span><span class="token punctuation">;</span>

    <span class="token comment">// 文件所在组</span>
    perms<span class="token punctuation">[</span><span class="token number">4</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>st<span class="token punctuation">.</span>st_mode <span class="token operator">&amp;</span> S_IRGRP<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">'r'</span> <span class="token operator">:</span> <span class="token string">'-'</span><span class="token punctuation">;</span>
    perms<span class="token punctuation">[</span><span class="token number">5</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>st<span class="token punctuation">.</span>st_mode <span class="token operator">&amp;</span> S_IWGRP<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">'w'</span> <span class="token operator">:</span> <span class="token string">'-'</span><span class="token punctuation">;</span>
    perms<span class="token punctuation">[</span><span class="token number">6</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>st<span class="token punctuation">.</span>st_mode <span class="token operator">&amp;</span> S_IXGRP<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">'x'</span> <span class="token operator">:</span> <span class="token string">'-'</span><span class="token punctuation">;</span>

    <span class="token comment">// 其他人</span>
    perms<span class="token punctuation">[</span><span class="token number">7</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>st<span class="token punctuation">.</span>st_mode <span class="token operator">&amp;</span> S_IROTH<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">'r'</span> <span class="token operator">:</span> <span class="token string">'-'</span><span class="token punctuation">;</span>
    perms<span class="token punctuation">[</span><span class="token number">8</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>st<span class="token punctuation">.</span>st_mode <span class="token operator">&amp;</span> S_IWOTH<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">'w'</span> <span class="token operator">:</span> <span class="token string">'-'</span><span class="token punctuation">;</span>
    perms<span class="token punctuation">[</span><span class="token number">9</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">(</span>st<span class="token punctuation">.</span>st_mode <span class="token operator">&amp;</span> S_IXOTH<span class="token punctuation">)</span> <span class="token operator">?</span> <span class="token string">'x'</span> <span class="token operator">:</span> <span class="token string">'-'</span><span class="token punctuation">;</span>

    <span class="token comment">// 硬连接数</span>
    <span class="token keyword">int</span> linkNum <span class="token operator">=</span> st<span class="token punctuation">.</span>st_nlink<span class="token punctuation">;</span>

    <span class="token comment">// 文件所有者</span>
    <span class="token keyword">char</span> <span class="token operator">*</span> fileUser <span class="token operator">=</span> <span class="token function">getpwuid</span><span class="token punctuation">(</span>st<span class="token punctuation">.</span>st_uid<span class="token punctuation">)</span><span class="token operator">-&gt;</span>pw_name<span class="token punctuation">;</span>

    <span class="token comment">// 文件所在组</span>
    <span class="token keyword">char</span> <span class="token operator">*</span> fileGrp <span class="token operator">=</span> <span class="token function">getgrgid</span><span class="token punctuation">(</span>st<span class="token punctuation">.</span>st_gid<span class="token punctuation">)</span><span class="token operator">-&gt;</span>gr_name<span class="token punctuation">;</span>

    <span class="token comment">// 文件大小</span>
    <span class="token keyword">long</span> <span class="token keyword">int</span> fileSize <span class="token operator">=</span> st<span class="token punctuation">.</span>st_size<span class="token punctuation">;</span>

    <span class="token comment">// 获取修改的时间</span>
    <span class="token keyword">char</span> <span class="token operator">*</span> time <span class="token operator">=</span> <span class="token function">ctime</span><span class="token punctuation">(</span><span class="token operator">&amp;</span>st<span class="token punctuation">.</span>st_mtime<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">char</span> mtime<span class="token punctuation">[</span><span class="token number">512</span><span class="token punctuation">]</span> <span class="token operator">=</span> <span class="token punctuation">{</span><span class="token number">0</span><span class="token punctuation">}</span><span class="token punctuation">;</span>
    <span class="token function">strncpy</span><span class="token punctuation">(</span>mtime<span class="token punctuation">,</span> time<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>time<span class="token punctuation">)</span> <span class="token operator">-</span> <span class="token number">1</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">char</span> buf<span class="token punctuation">[</span><span class="token number">1024</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token function">sprintf</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span> <span class="token string">&quot;%s %d %s %s %ld %s %s&quot;</span><span class="token punctuation">,</span> perms<span class="token punctuation">,</span> linkNum<span class="token punctuation">,</span> fileUser<span class="token punctuation">,</span> fileGrp<span class="token punctuation">,</span> fileSize<span class="token punctuation">,</span> mtime<span class="token punctuation">,</span> argv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s\n&quot;</span><span class="token punctuation">,</span> buf<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br><span class="line-number">97</span><br><span class="line-number">98</span><br><span class="line-number">99</span><br><span class="line-number">100</span><br></div></div><h2 id="_26-文件属性操作函数"><a href="#_26-文件属性操作函数" class="header-anchor">#</a> 26.文件属性操作函数</h2> <h2 id="_27-目录操作函数"><a href="#_27-目录操作函数" class="header-anchor">#</a> 27.目录操作函数</h2> <h4 id="『1』chdir-c"><a href="#『1』chdir-c" class="header-anchor">#</a> 『1』chdir.c</h4> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/*

    #include &lt;unistd.h&gt;
    int chdir(const char *path);
        作用：修改进程的工作目录
            比如在/home/nowcoder 启动了一个可执行程序a.out, 进程的工作目录 /home/nowcoder
        参数：
            path : 需要修改的工作目录

    #include &lt;unistd.h&gt;
    char *getcwd(char *buf, size_t size);
        作用：获取当前工作目录
        参数：
            - buf : 存储的路径，指向的是一个数组（传出参数）
            - size: 数组的大小
        返回值：
            返回的指向的一块内存，这个数据就是第一个参数

*/</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/stat.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fcntl.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 获取当前的工作目录</span>
    <span class="token keyword">char</span> buf<span class="token punctuation">[</span><span class="token number">128</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token function">getcwd</span><span class="token punctuation">(</span>buf<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>buf<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;当前的工作目录是：%s\n&quot;</span><span class="token punctuation">,</span> buf<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 修改工作目录</span>
    <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">chdir</span><span class="token punctuation">(</span><span class="token string">&quot;/home/nowcoder/Linux/lesson13&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>ret <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;chdir&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span> 

    <span class="token comment">// 创建一个新的文件</span>
    <span class="token keyword">int</span> fd <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;chdir.txt&quot;</span><span class="token punctuation">,</span> O_CREAT <span class="token operator">|</span> O_RDWR<span class="token punctuation">,</span> <span class="token number">0664</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>fd <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;open&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">close</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token comment">// 获取当前的工作目录</span>
    <span class="token keyword">char</span> buf1<span class="token punctuation">[</span><span class="token number">128</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
    <span class="token function">getcwd</span><span class="token punctuation">(</span>buf1<span class="token punctuation">,</span> <span class="token keyword">sizeof</span><span class="token punctuation">(</span>buf1<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;当前的工作目录是：%s\n&quot;</span><span class="token punctuation">,</span> buf1<span class="token punctuation">)</span><span class="token punctuation">;</span>
    
    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br></div></div><h4 id="『2』mkdir-c"><a href="#『2』mkdir-c" class="header-anchor">#</a> 『2』mkdir.c</h4> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/*
    #include &lt;sys/stat.h&gt;
    #include &lt;sys/types.h&gt;
    int mkdir(const char *pathname, mode_t mode);
        作用：创建一个目录
        参数：
            pathname: 创建的目录的路径
            mode: 权限，八进制的数
        返回值：
            成功返回0， 失败返回-1
*/</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/stat.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">mkdir</span><span class="token punctuation">(</span><span class="token string">&quot;aaa&quot;</span><span class="token punctuation">,</span> <span class="token number">0777</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>ret <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;mkdir&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br></div></div><h4 id="『3』rename-c"><a href="#『3』rename-c" class="header-anchor">#</a> 『3』rename.c</h4> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/*
    #include &lt;stdio.h&gt;
    int rename(const char *oldpath, const char *newpath);

*/</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">rename</span><span class="token punctuation">(</span><span class="token string">&quot;aaa&quot;</span><span class="token punctuation">,</span> <span class="token string">&quot;bbb&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>ret <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;rename&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br></div></div><h2 id="_28-目录遍历函数"><a href="#_28-目录遍历函数" class="header-anchor">#</a> 28-目录遍历函数</h2> <blockquote><p>readFileNum.c</p></blockquote> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/*
    // 打开一个目录
    #include &lt;sys/types.h&gt;
    #include &lt;dirent.h&gt;
    DIR *opendir(const char *name);
        参数：
            - name: 需要打开的目录的名称
        返回值：
            DIR * 类型，理解为目录流
            错误返回NULL


    // 读取目录中的数据
    #include &lt;dirent.h&gt;
    struct dirent *readdir(DIR *dirp);
        - 参数：dirp是opendir返回的结果
        - 返回值：
            struct dirent，代表读取到的文件的信息
            读取到了末尾或者失败了，返回NULL

    // 关闭目录
    #include &lt;sys/types.h&gt;
    #include &lt;dirent.h&gt;
    int closedir(DIR *dirp);

*/</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;dirent.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdlib.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">getFileNum</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span> path<span class="token punctuation">)</span><span class="token punctuation">;</span>

<span class="token comment">// 读取某个目录下所有的普通文件的个数</span>
<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token keyword">int</span> argc<span class="token punctuation">,</span> <span class="token keyword">char</span> <span class="token operator">*</span> argv<span class="token punctuation">[</span><span class="token punctuation">]</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>argc <span class="token operator">&lt;</span> <span class="token number">2</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;%s path\n&quot;</span><span class="token punctuation">,</span> argv<span class="token punctuation">[</span><span class="token number">0</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">int</span> num <span class="token operator">=</span> <span class="token function">getFileNum</span><span class="token punctuation">(</span>argv<span class="token punctuation">[</span><span class="token number">1</span><span class="token punctuation">]</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;普通文件的个数为：%d\n&quot;</span><span class="token punctuation">,</span> num<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>

<span class="token comment">// 用于获取目录下所有普通文件的个数</span>
<span class="token keyword">int</span> <span class="token function">getFileNum</span><span class="token punctuation">(</span><span class="token keyword">const</span> <span class="token keyword">char</span> <span class="token operator">*</span> path<span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 1.打开目录</span>
    DIR <span class="token operator">*</span> dir <span class="token operator">=</span> <span class="token function">opendir</span><span class="token punctuation">(</span>path<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>dir <span class="token operator">==</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;opendir&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token function">exit</span><span class="token punctuation">(</span><span class="token number">0</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">struct</span> <span class="token class-name">dirent</span> <span class="token operator">*</span>ptr<span class="token punctuation">;</span>

    <span class="token comment">// 记录普通文件的个数</span>
    <span class="token keyword">int</span> total <span class="token operator">=</span> <span class="token number">0</span><span class="token punctuation">;</span>

    <span class="token keyword">while</span><span class="token punctuation">(</span><span class="token punctuation">(</span>ptr <span class="token operator">=</span> <span class="token function">readdir</span><span class="token punctuation">(</span>dir<span class="token punctuation">)</span><span class="token punctuation">)</span> <span class="token operator">!=</span> <span class="token constant">NULL</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

        <span class="token comment">// 获取名称</span>
        <span class="token keyword">char</span> <span class="token operator">*</span> dname <span class="token operator">=</span> ptr<span class="token operator">-&gt;</span>d_name<span class="token punctuation">;</span>

        <span class="token comment">// 忽略掉. 和..</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span><span class="token function">strcmp</span><span class="token punctuation">(</span>dname<span class="token punctuation">,</span> <span class="token string">&quot;.&quot;</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span> <span class="token operator">||</span> <span class="token function">strcmp</span><span class="token punctuation">(</span>dname<span class="token punctuation">,</span> <span class="token string">&quot;..&quot;</span><span class="token punctuation">)</span> <span class="token operator">==</span> <span class="token number">0</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token keyword">continue</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token comment">// 判断是否是普通文件还是目录</span>
        <span class="token keyword">if</span><span class="token punctuation">(</span>ptr<span class="token operator">-&gt;</span>d_type <span class="token operator">==</span> DT_DIR<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// 目录,需要继续读取这个目录</span>
            <span class="token keyword">char</span> newpath<span class="token punctuation">[</span><span class="token number">256</span><span class="token punctuation">]</span><span class="token punctuation">;</span>
            <span class="token function">sprintf</span><span class="token punctuation">(</span>newpath<span class="token punctuation">,</span> <span class="token string">&quot;%s/%s&quot;</span><span class="token punctuation">,</span> path<span class="token punctuation">,</span> dname<span class="token punctuation">)</span><span class="token punctuation">;</span>
            total <span class="token operator">+=</span> <span class="token function">getFileNum</span><span class="token punctuation">(</span>newpath<span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>

        <span class="token keyword">if</span><span class="token punctuation">(</span>ptr<span class="token operator">-&gt;</span>d_type <span class="token operator">==</span> DT_REG<span class="token punctuation">)</span> <span class="token punctuation">{</span>
            <span class="token comment">// 普通文件</span>
            total<span class="token operator">++</span><span class="token punctuation">;</span>
        <span class="token punctuation">}</span>


    <span class="token punctuation">}</span>

    <span class="token comment">// 关闭目录</span>
    <span class="token function">closedir</span><span class="token punctuation">(</span>dir<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> total<span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br><span class="line-number">64</span><br><span class="line-number">65</span><br><span class="line-number">66</span><br><span class="line-number">67</span><br><span class="line-number">68</span><br><span class="line-number">69</span><br><span class="line-number">70</span><br><span class="line-number">71</span><br><span class="line-number">72</span><br><span class="line-number">73</span><br><span class="line-number">74</span><br><span class="line-number">75</span><br><span class="line-number">76</span><br><span class="line-number">77</span><br><span class="line-number">78</span><br><span class="line-number">79</span><br><span class="line-number">80</span><br><span class="line-number">81</span><br><span class="line-number">82</span><br><span class="line-number">83</span><br><span class="line-number">84</span><br><span class="line-number">85</span><br><span class="line-number">86</span><br><span class="line-number">87</span><br><span class="line-number">88</span><br><span class="line-number">89</span><br><span class="line-number">90</span><br><span class="line-number">91</span><br><span class="line-number">92</span><br><span class="line-number">93</span><br><span class="line-number">94</span><br><span class="line-number">95</span><br><span class="line-number">96</span><br></div></div><h2 id="_29-dup和dup2函数"><a href="#_29-dup和dup2函数" class="header-anchor">#</a> 29.<code>dup</code>和<code>dup2</code>函数</h2> <ul><li>《TCP/IP网络编程》书上是用这个『在同一个进程中“复制”（其实是用另一个文件符来模仿）这个文件符，然后好实现，“半关闭”啥的</li></ul> <h4 id="『1』dup-c"><a href="#『1』dup-c" class="header-anchor">#</a> 『1』dup.c</h4> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/*
    #include &lt;unistd.h&gt;
    int dup(int oldfd);
        作用：复制一个新的文件描述符
        fd=3, int fd1 = dup(fd),
        fd指向的是a.txt, fd1也是指向a.txt
        从空闲的文件描述符表中找一个最小的，作为新的拷贝的文件描述符


*/</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fcntl.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/stat.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">int</span> fd <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;a.txt&quot;</span><span class="token punctuation">,</span> O_RDWR <span class="token operator">|</span> O_CREAT<span class="token punctuation">,</span> <span class="token number">0664</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">int</span> fd1 <span class="token operator">=</span> <span class="token function">dup</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>fd1 <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;dup&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;fd : %d , fd1 : %d\n&quot;</span><span class="token punctuation">,</span> fd<span class="token punctuation">,</span> fd1<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">close</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">char</span> <span class="token operator">*</span> str <span class="token operator">=</span> <span class="token string">&quot;hello,world&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">write</span><span class="token punctuation">(</span>fd1<span class="token punctuation">,</span> str<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>ret <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;write&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">close</span><span class="token punctuation">(</span>fd1<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br></div></div><h4 id="『2』dup2-c"><a href="#『2』dup2-c" class="header-anchor">#</a> 『2』dup2.c</h4> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/*
    #include &lt;unistd.h&gt;
    int dup2(int oldfd, int newfd);
        作用：重定向文件描述符
        oldfd 指向 a.txt, newfd 指向 b.txt
        调用函数成功后：newfd 和 b.txt 做close, newfd 指向了 a.txt
        oldfd 必须是一个有效的文件描述符
        oldfd和newfd值相同，相当于什么都没有做
*/</span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/stat.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;sys/types.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fcntl.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token keyword">int</span> fd <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;1.txt&quot;</span><span class="token punctuation">,</span> O_RDWR <span class="token operator">|</span> O_CREAT<span class="token punctuation">,</span> <span class="token number">0664</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>fd <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;open&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">int</span> fd1 <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;2.txt&quot;</span><span class="token punctuation">,</span> O_RDWR <span class="token operator">|</span> O_CREAT<span class="token punctuation">,</span> <span class="token number">0664</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>fd1 <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;open&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;fd : %d, fd1 : %d\n&quot;</span><span class="token punctuation">,</span> fd<span class="token punctuation">,</span> fd1<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">int</span> fd2 <span class="token operator">=</span> <span class="token function">dup2</span><span class="token punctuation">(</span>fd<span class="token punctuation">,</span> fd1<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>fd2 <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;dup2&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 通过fd1去写数据，实际操作的是1.txt，而不是2.txt</span>
    <span class="token keyword">char</span> <span class="token operator">*</span> str <span class="token operator">=</span> <span class="token string">&quot;hello, dup2&quot;</span><span class="token punctuation">;</span>
    <span class="token keyword">int</span> len <span class="token operator">=</span> <span class="token function">write</span><span class="token punctuation">(</span>fd1<span class="token punctuation">,</span> str<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">if</span><span class="token punctuation">(</span>len <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;write&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token function">printf</span><span class="token punctuation">(</span><span class="token string">&quot;fd : %d, fd1 : %d, fd2 : %d\n&quot;</span><span class="token punctuation">,</span> fd<span class="token punctuation">,</span> fd1<span class="token punctuation">,</span> fd2<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token function">close</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">close</span><span class="token punctuation">(</span>fd1<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br></div></div><h2 id="_30-fcntl函数"><a href="#_30-fcntl函数" class="header-anchor">#</a> 30.<code>fcntl</code>函数</h2> <ul><li>《TCP/IP网络编程》书中对于这个是用，那个『好像是“紧急传输数据”，但是TCP不支持233』</li></ul> <blockquote><p>fcntl.c</p></blockquote> <div class="language-c line-numbers-mode"><pre class="language-c"><code><span class="token comment">/*

    #include &lt;unistd.h&gt;
    #include &lt;fcntl.h&gt;

    int fcntl(int fd, int cmd, ...);
    参数：
        fd : 表示需要操作的文件描述符
        cmd: 表示对文件描述符进行如何操作
            - F_DUPFD : 复制文件描述符,复制的是第一个参数fd，得到一个新的文件描述符（返回值）
                int ret = fcntl(fd, F_DUPFD);

            - F_GETFL : 获取指定的文件描述符文件状态flag
              获取的flag和我们通过open函数传递的flag是一个东西。

            - F_SETFL : 设置文件描述符文件状态flag
              必选项：O_RDONLY, O_WRONLY, O_RDWR 不可以被修改
              可选性：O_APPEND, O)NONBLOCK
                O_APPEND 表示追加数据
                NONBLOK 设置成非阻塞
        
        阻塞和非阻塞：描述的是函数调用的行为。
*/</span>

<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;unistd.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;fcntl.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;stdio.h&gt;</span></span>
<span class="token macro property"><span class="token directive-hash">#</span><span class="token directive keyword">include</span> <span class="token string">&lt;string.h&gt;</span></span>

<span class="token keyword">int</span> <span class="token function">main</span><span class="token punctuation">(</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>

    <span class="token comment">// 1.复制文件描述符</span>
    <span class="token comment">// int fd = open(&quot;1.txt&quot;, O_RDONLY);</span>
    <span class="token comment">// int ret = fcntl(fd, F_DUPFD);</span>

    <span class="token comment">// 2.修改或者获取文件状态flag</span>
    <span class="token keyword">int</span> fd <span class="token operator">=</span> <span class="token function">open</span><span class="token punctuation">(</span><span class="token string">&quot;1.txt&quot;</span><span class="token punctuation">,</span> O_RDWR<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>fd <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;open&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token comment">// 获取文件描述符状态flag</span>
    <span class="token keyword">int</span> flag <span class="token operator">=</span> <span class="token function">fcntl</span><span class="token punctuation">(</span>fd<span class="token punctuation">,</span> F_GETFL<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>flag <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;fcntl&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>
    flag <span class="token operator">|=</span> O_APPEND<span class="token punctuation">;</span>   <span class="token comment">// flag = flag | O_APPEND</span>

    <span class="token comment">// 修改文件描述符状态的flag，给flag加入O_APPEND这个标记</span>
    <span class="token keyword">int</span> ret <span class="token operator">=</span> <span class="token function">fcntl</span><span class="token punctuation">(</span>fd<span class="token punctuation">,</span> F_SETFL<span class="token punctuation">,</span> flag<span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token keyword">if</span><span class="token punctuation">(</span>ret <span class="token operator">==</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">)</span> <span class="token punctuation">{</span>
        <span class="token function">perror</span><span class="token punctuation">(</span><span class="token string">&quot;fcntl&quot;</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
        <span class="token keyword">return</span> <span class="token operator">-</span><span class="token number">1</span><span class="token punctuation">;</span>
    <span class="token punctuation">}</span>

    <span class="token keyword">char</span> <span class="token operator">*</span> str <span class="token operator">=</span> <span class="token string">&quot;nihao&quot;</span><span class="token punctuation">;</span>
    <span class="token function">write</span><span class="token punctuation">(</span>fd<span class="token punctuation">,</span> str<span class="token punctuation">,</span> <span class="token function">strlen</span><span class="token punctuation">(</span>str<span class="token punctuation">)</span><span class="token punctuation">)</span><span class="token punctuation">;</span>
    <span class="token function">close</span><span class="token punctuation">(</span>fd<span class="token punctuation">)</span><span class="token punctuation">;</span>

    <span class="token keyword">return</span> <span class="token number">0</span><span class="token punctuation">;</span>
<span class="token punctuation">}</span>
</code></pre> <div class="line-numbers-wrapper"><span class="line-number">1</span><br><span class="line-number">2</span><br><span class="line-number">3</span><br><span class="line-number">4</span><br><span class="line-number">5</span><br><span class="line-number">6</span><br><span class="line-number">7</span><br><span class="line-number">8</span><br><span class="line-number">9</span><br><span class="line-number">10</span><br><span class="line-number">11</span><br><span class="line-number">12</span><br><span class="line-number">13</span><br><span class="line-number">14</span><br><span class="line-number">15</span><br><span class="line-number">16</span><br><span class="line-number">17</span><br><span class="line-number">18</span><br><span class="line-number">19</span><br><span class="line-number">20</span><br><span class="line-number">21</span><br><span class="line-number">22</span><br><span class="line-number">23</span><br><span class="line-number">24</span><br><span class="line-number">25</span><br><span class="line-number">26</span><br><span class="line-number">27</span><br><span class="line-number">28</span><br><span class="line-number">29</span><br><span class="line-number">30</span><br><span class="line-number">31</span><br><span class="line-number">32</span><br><span class="line-number">33</span><br><span class="line-number">34</span><br><span class="line-number">35</span><br><span class="line-number">36</span><br><span class="line-number">37</span><br><span class="line-number">38</span><br><span class="line-number">39</span><br><span class="line-number">40</span><br><span class="line-number">41</span><br><span class="line-number">42</span><br><span class="line-number">43</span><br><span class="line-number">44</span><br><span class="line-number">45</span><br><span class="line-number">46</span><br><span class="line-number">47</span><br><span class="line-number">48</span><br><span class="line-number">49</span><br><span class="line-number">50</span><br><span class="line-number">51</span><br><span class="line-number">52</span><br><span class="line-number">53</span><br><span class="line-number">54</span><br><span class="line-number">55</span><br><span class="line-number">56</span><br><span class="line-number">57</span><br><span class="line-number">58</span><br><span class="line-number">59</span><br><span class="line-number">60</span><br><span class="line-number">61</span><br><span class="line-number">62</span><br><span class="line-number">63</span><br></div></div></div> <div class="page-edit"><!----> <!----></div> <!----> </div> <!----></div></div>
    <script src="/assets/js/app.c70e21ad.js" defer></script><script src="/assets/js/72.e5467385.js" defer></script>
  </body>
</html>
